_id的Mongoose findOne嵌入式文档 [英] Mongoose findOne embedded document by _id

查看:80
本文介绍了_id的Mongoose findOne嵌入式文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将菜单推送到嵌入式文档.但是我在餐厅没有定义findOne.我只想将一些文档推送到餐厅的菜单类别中.如您在架构中所见:

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:

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);
            });
        }
    });
}

推荐答案

子文档具有.id()方法,因此您可以执行以下操作:

Subdocuments have an .id() method so you can do this:

myModel.findById(myDocumentId, function (err, myDocument) {
  var subDocument = myDocument.mySubdocuments.id(mySubDocumentId);
});

请参见 http://mongoosejs.com/docs/subdocs.html 以供参考.

这篇关于_id的Mongoose findOne嵌入式文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆