node.js Mongoose 通过 _id findOne 嵌入文档
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20629127/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mongoose findOne embedded document by _id
提问by yong ho
I am trying to push menus to the embedded document. But I am getting not defined findOne in restaurant. I just want to push some documents into the restaurant's menu categories. As you can see in the schema:
我正在尝试将菜单推送到嵌入式文档。但是我在餐厅中没有定义 findOne 。我只想将一些文档推送到餐厅的菜单类别中。正如您在架构中看到的:
var RestaurantSchema = new mongoose.Schema({
contactTelphone : String,
address : String,
branchID : String,
email : String,
restaurantName : String,
userID : String,
menuCategory : [MenuCategorySchema]
});
var MenuCategorySchema = new mongoose.Schema({
menuCategory : String,
menuCategoryAlt : String,
sequence : Number,
menus : [MenuSchema],
restaurantInfo : { type: Schema.Types.ObjectId, ref: 'Restaurant' },
});
var MenuSchema = new mongoose.Schema({
foodName : String,
foodNameAlt : String,
picName : String,
price : String,
rating : Number,
menuSequence : Number,
category : { type: Schema.Types.ObjectId, ref: 'MenuCategory' },
});
exports.menuForMenuCategory = function(newData, callback)
{
console.log(newData);
var menuCategoryId = newData.menuCategoryId;
var restaurantId = newData.restaurantId;
var newMenu = new Menu({
foodName : newData.foodName,
foodNameAlt : newData.foodNameAlt,
picName : newData.picName,
price : newData.price,
cookingCategory : newCookingCategory,
dishSpecial : newDishSpeical
});
Restaurant.findOne( {'_id' : restaurantId }, function(err, restaurant){
if (!err) {
//Is it how to do this? It says "findOne not defined"
restaurant.findOne( "_id" : menuCategoryId, function(err, category){
category.push(newMenu);
});
}
});
}
回答by Paul Verhoeven
Subdocuments have an .id() method so you can do this:
子文档有一个 .id() 方法,所以你可以这样做:
myModel.findById(myDocumentId, function (err, myDocument) {
var subDocument = myDocument.mySubdocuments.id(mySubDocumentId);
});
See http://mongoosejs.com/docs/subdocs.htmlfor reference.
回答by C Blanchard
restaurantis just an object containing the result to your query. It does not have a findOne method, only Restaurantdoes.
restaurant只是一个包含查询结果的对象。它没有 findOne 方法,只有Restaurant。
Since, MenuCategoryis just a sub-document of Restaurant, this will come pre-populated whenever you retrieve a restaurant. E.g.
由于,MenuCategory只是 的子文档Restaurant,因此每当您检索餐厅时都会预先填充它。例如
Restaurant.findById(restaurantId, function(err, restaurant){
console.log(restaurant.menuCategory);
// Will show your array of Menu Categories
// No further queries required
});
Adding a new Menu Category is a matter of pushing a new MenuCategoryinstance to the menuCategoryarray and saving the restaurant. This means the new menu category is saved with the restaurantand not in a separate collection. For example:
添加新的菜单类别是将新MenuCategory实例推送到menuCategory数组并保存餐厅的问题。这意味着新菜单类别与餐厅一起保存,而不是在单独的集合中。例如:
Restaurant.findById(restaurantId, function(err, restaurant){
// I'm assuming your Menu Category model is just MenuCategory
var anotherMenuCategory = new MenuCategory({
menuCategory: "The name",
menuCategoryAlt: "Alternative name",
sequence: 42,
menus: []
});
restaurant.menuCategory.push(anotherMenuCategory);
restaurant.save(); // This will save the new Menu Category in your restaurant
});
Saving a menu to a menu category follows the same procedure since, according to your schema, Menus are embedded sub-documents within each MenuCategory. But note that you need to save the restaurantas its the restaurant collection that is storing all your menus and menu categories as sub-documents
将菜单保存到菜单类别遵循相同的过程,因为根据您的架构,菜单是每个 MenuCategory 中嵌入的子文档。但请注意,您需要将餐厅保存为将所有菜单和菜单类别存储为子文档的餐厅集合
Having answered your question (I hope) I should also point out your schema design should be rethought. Having sub-documents nested within sub-documents is arguably not a good idea. I think I can see where you're coming from - you're trying to implement a SQL-like many-to-one association within your schemas. But this isn't necessary with NoSQL databases - the thinking is somewhat different. Here are some links to some SO questions about efficient schema design with NoSQL databases:
回答了您的问题(我希望)后,我还应该指出应该重新考虑您的架构设计。将子文档嵌套在子文档中可能不是一个好主意。我想我可以看到您来自哪里 - 您正在尝试在您的架构中实现类似于 SQL 的多对一关联。但这对于 NoSQL 数据库来说不是必需的——想法有些不同。以下是一些有关使用 NoSQL 数据库进行高效模式设计的 SO 问题的链接:

