使用猫鼬在多模型集合中查找模型

时间:2019-06-16 14:56:06

标签: javascript node.js mongoose mongoose-schema

我有一个包含单个文档的集合,其中包含许多模型类型。

json的一般格式:

{
    _id: Id;
    ingredients: {...};
    products: {...};
    recipes: {...};
}

我已经定义了模式

var RecipeSchema = new mongoose.Schema({...});
var MenuSchema = new mongoose.Schema({...});
var IngredientSchema = new mongoose.Schema({});
var ProductSchema = new mongoose.Schema({});

然后模型

var Recipe = mongoose.model('Recipe', RecipeSchema, 'recipes'); // I specify the collection name
var Menu = mongoose.model('Menu', MenuSchema);
var Ingredient = mongoose.model('Ingredient', IngredientSchema);
var Product = mongoose.model('Product', ProductSchema);

module.exports = {
  Recipe: Recipe,
  Menu: Menu,
  Ingredient: Ingredient,
  Product: Product
  };

然后我获取数据

const docs = await models.Recipe.find();
        res.json(docs);

我得到的答复是包含所有文档数据。我所得到的只是recipes项(带有孩子),而不是其他项(配料,产品等)

这就是我得到的What I get

[  
   {  
      "name":"",
      "tags":[  

      ],
      "_id":"5d042b66e7179a4e4324eac8",
      "ingredients":[ ... ],
      "products":[ ... ],
      "recipes":[ ... ],
      "ingredients_list":[  

      ]
   }
]

我在doc元素内部获得了所有文档内容,但第一级中的其他项是undefined

如何仅检索文档的一部分recipes

0 个答案:

没有答案
相关问题