2016-11-15 82 views
0

我正在使用node.js工作mongodb連接查詢,我有兩個集合,我想加入兩個集合並希望填充數據。如何使用node.js連接Mongodb中的集合?

飲料收集

[{ 
    "_id": "580a2f7615a0161010a23bfg", 
    "name": "Pepsi", 
    "createDate": "2016-10-21T15:08:27.405Z" 
}, { 
    "_id": "580a2f7615a0161010bge345", 
    "name": "Mirinda", 
    "createDate": "2016-10-21T15:08:27.405Z" 
}, { 
    "_id": "580a2f7615a0161010vbgdt5", 
    "name": "Coke", 
    "createDate": "2016-10-21T15:08:27.405Z" 
}, { 
    "_id": "580a2f7615a0161010vb9876", 
    "name": "Thums Up", 
    "createDate": "2016-10-21T15:08:27.405Z" 
}, { 
    "_id": "580a2f7615a0161010vbgmnk9", 
    "name": "Slice", 
    "createDate": "2016-10-21T15:08:27.405Z" 
}] 

指令集

[{ 
    "_id": "580fa9a92bb22e783b298798", 
    "created_at": "2016-10-25T18:41:53.716Z", 
    "tab": [{ 
     "created_at": "2016-10-26T00:21:33.100Z", 
     "cost": 30, 
     "quantity": 7, 
     "name": "Mirinda" 
}] 
}, 
{ 
    "_id": "580fa9a92bb22e783b29887h", 
    "created_at": "2016-10-25T18:41:53.716Z", 
    "tab": [{ 
     "created_at": "2016-10-26T00:21:33.100Z", 
     "cost": 25, 
     "quantity": 5, 
     "name": "Pepsi" 
    }] 
}] 

所以我想通過加入飲料收集找到指令集的名稱。

訂單模式

var mongoose = require("mongoose"); 

var OrderSchema = new mongoose.Schema({ 
    tab: [{ 
     quantity: Number, 
     cost: Number, 
     name: { 
      type: String 
     }, 
     created_at: { 
      type: Date 
     } 
    }], 
}); 
module.exports = mongoose.model("Order", OrderSchema); 

飲料架構

var mongoose = require('mongoose'), 
utilities = require('../config/utilities'); 

var DrinkSchema = mongoose.Schema({ 
    drinks_name: { type: mongoose.Schema.Types.ObjectId, ref: 'Order' }, 
    name: { 
    type: String 
    }, 
    id: { 
    type: String 
    }, 
    createDate: { 
    type: String 
    }, 
}) 
module.exports = mongoose.model("Drinks", DrinkSchema); 

Node.js的代碼

var Drinks = require("../models/drinks"), 
Orders = require("../models/orders"), 
async = require('async'), 
mongoose = require('mongoose'); 

exports.getOrdersList = function(req, res) { 

    //How to Join Drinks and Orders Collection ? 
    Drinks 
    .find({}) 
    .populate('drinks_name', 'tab.name') 
    .exec(function (err, result) { 
     console.log('The Drinks List is', result); 
    }); 
} 

敬請通過我上面的帖子,讓我知道,如果您有任何解決方案。

+0

我不確定在MongoDB中是否有允許連接的內置功能。 – Thilo

+0

嘿我猜聚合查詢將工作 –

+1

通過代碼執行加入或檢查貓鼬引用和人口。 – qqilihq

回答

0

您無法在MongoDB中創建聯接,因爲它是NoSQL數據庫。我的理解是,你想根據'訂單'的名稱字段從'Drinks'中檢索數據。你可以做的是找到訂單集合中每個文檔的'tab.name'字段,然後從Drinks集合中找到'_id'字段。

Orders.find({"tab.name":"Mirinda"}); 
Drinks.find({"name":"Mirinda"}), { "_id" : 1", "created_at":1}); 

然後,您可以串聯兩個結果或多或少得到你想要的東西。

+0

官方沒有加入,但是當他使用貓鼬他可以與填充選項..但他正在尋找填充數據類型數組。 –

+0

@BEJGAMSHIVAPRASAD嘿謝謝,是的,我正在尋找填充查詢可以請你幫我嗎? –

+0

我已經使用填充,但..沒有嘗試數組對象..我會檢查並更新你.. –

0

連接並不在MongoDB的原生支持,但是我們可以實現連接兩種不同的方式方法:

  • 嵌入對象的參考對象的引用文件
  • 貯藏_id(在文檔本身嵌入的參考對象)。

如果已經使用貓鼬ODM創建的模式,你可以獲取使用

Drinks.find({}).populate('orders').exec(cb)參考對象。

==============================

這裏是在架構訂單和飲料略有變化。你不需要有一個成本領域,因爲它可以根據每種飲料的數量來計算。也可以查找填充查詢。

var mongoose = require('mongoose'); 
var Schema = mongoose.Schema; 

var Orders = new Schema({ 
    waiter: {type: String}, 
    table: {type: Number}, 
    items: [ 
     { 
      quantity: {type: Number, default: 1 }, 
      drink_id: {type: mongoose.Schema.Types.ObjectId, ref: 'Drinks'} 
     } 
    ], 
    billed: false, 
    timestamp : { 
     createdAt: created_at, 
     updatedAt: updated_at 
    } 
}) 

module.exports = mongoose.model('Order', Orders); 


var Drink = new Schema({ 
    name: {type: String, required: true}, 
    cost: {type: Number, required: true} 
    discount: { type: Number}, 
    timestamps: { 
     createdAt: created_at, 
     updatedAt: updated_at 
    } 
}) 


module.exports = mongoose.model('Drink', Drink); 


============ 

For Querying : 

var Orders = require('./Orders.js'); 

Orders.find({}).populate('drinks').exec(function(err, documents){ 
    // you will get drinks object in response 
}) 

你將不得不編寫一個查詢,如果以特定數量的更新訂單反映,你可以使用聚合框架來計算成本的順序特定表更新的順序。

+0

@MyChat關於更新飲料數量的最後一點,你可以在mongo上使用$ in運算符找到 –

+0

@MyChat我的bad $ in將不起作用,但$ elemMatch會在搜索對象數組時使用它 –

0

看起來你的問題是錯誤的...你需要獲得訂單信息以及物品,但物品名稱應該從飲料收集中填充..

訂單模式:

var mongoose = require("mongoose"); 
var ObjectId = mongoose.Schema.Types.ObjectId; 

var OrderSchema = new mongoose.Schema({ 
    tab: [{ 
     itemId:{ 
     type: ObjectId, 
     ref: 'Drinks', 
     required: true 
     }, 
     quantity: Number, 
     cost: Number, 
     created_at: { 
      type: Date, 
      default:Date.now 
     } 
    }], 
    created_at: { 
      type: Date, 
      default:Date.now 
     } 

}); 
module.exports = mongoose.model("Order", OrderSchema); 

飲料架構

var mongoose = require('mongoose'); 
var DrinkSchema = mongoose.Schema({ 
    drinks_name: { type: mongoose.Schema.Types.ObjectId, ref: 'Order'}, 
    name: { 
    type: String 
    }, 
    createDate: { 
    type: Date, 
    default: Date.now 
    }, 
}); 
module.exports = mongoose.model("Drinks", DrinkSchema); 


節點代碼:

var Drinks = require("./drinks"), 
Orders = require("./orders"), 
mongoose = require('mongoose'); 
/* mongoose.connect('mongodb://connectionsrint'); */ 
    //How to Join Drinks and Orders Collection ? 
    Orders 
    .find({_id:'582ae63967190c92552f871b'}) 
    .populate('tab.itemId') 
    .exec(function (err, result) { 
    console.log(JSON.stringify(result)); 
    }); 

輸出

[{"_id":"582ae63967190c92552f871b","__v":0,"created_at":"2016-11-15T10:40:57.256Z","tab":[{"itemId":{"_id":"582ae4988bd47efe54bc5282","name":"Pepsi","__v":0,"createDate":"2016-11-15T10:34:00.115Z"},"quantity":5,"cost":125,"_id":"582ae63967190c92552f871c","created_at":"2016-11-15T10:40:57.262Z"}]}]