简体   繁体   English

如何使用猫鼬将一个集合的ID添加到另一个集合的ID数组

[英]how to add an id's of one collection to arrays of id of other collection using mongoose

I have 2 collections one is Products which looks like the following 我有2个收藏夹,其中一个是“产品”,如下所示

  var ProductSchema = new Schema({
    name: String,
    id:[parts_id],    // What should i change here to embed all the id's of parts collection in array 
   });

  var partSchema =new Schema(
    {
    "_id":autogenrated id",
    "price" : number,
    "properties" : {
    "battery type" : string,
    "talktime" : string,
    "weight" : number
    },
 });

 var Product = mongoose.model("Product");
 var product = new Product(

{
    "_id":"auto generated Id",
    "name" : "iphone 5",

 } 

 and other is parts which look like this 

 var parts = mongoose.model("parts");
 var parts = new parts(
 {
    "_id":autogenrated id",
    "price" : "$650",
    "properties" : {
    "battery type" : "lithium",
    "talktime" : "8 hours",
    "weight" : 2.3
  },
});

and these two collections have one to many relationship, many being the parts collection. 这两个集合具有一对多的关系,其中很多是零件集合。 I want to add the id's of parts collection into an array of products collection for referencing. 我想将零件集合的ID添加到产品集合数组中以供参考。 How can I do that using mongoose and what is the syntax for that 我如何使用猫鼬来做到这一点,它的语法是什么

Have you tried 你有没有尝试过

var ProductSchema = new Schema({
   name: String,
   parts:[{type:Schema.Types.ObjectId, ref: 'parts' }]],    
});

By the way, I think you need to include the schema when declaring the model: 顺便说一句,我认为您在声明模型时需要包括模式:

var parts  = mongoose.model('parts', partSchema);
var Product = mongoose.model('Product', ProductSchema);

Also, you should consider a consistent naming convention regarding casing and singular/plural forms of the names. 另外,您应考虑有关名称的大小写和单/复数形式的一致命名约定。

Since it looks like you are attempting a relational model with Mongoose , I'm including the documentation regarding .populate() . 由于看起来您正在尝试使用Mongoose建立关系模型,因此我包含了有关.populate()的文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用文档ID从mongoDB集合中删除文档? - How to delete document from mongoDB collection using the document's ID? mongoose快递节点JS链接二合集不使用_id - mongoose express node JS link two collection without using _id 如何使用骨干js将一个集合的单击项的ID设置为另一个集合的属性 - How to set the id of clicked item of one collection as attribute for another collection using backbone js 使用 mongoose 检查相关 mongodb 集合中是否存在 ID - Check if ID exist in related mongodb collection with mongoose 如果集合的数组在猫鼬中包含id,则选择集合 - Select collection if its array contains id in mongoose 我可以过滤另一个集合中具有_id并在mongodb中启用结果的集合吗 - Can i filter one collection that has _id in other collection and have result on in mongodb 使用_id集合流星作为整数 - Using _id collection Meteor as an integer 如何将 user.uid 作为 id 的文档添加到 Firebase 中的集合中? - How to add a document with user.uid as id to a collection in Firebase? 如何在Meteor中通过任意属性而不是_id查找一个集合项? - How to find one collection item by an arbitrary property, but not _id, in Meteor? Firebase 在创建集合时将自动生成的 id 保存到它的集合中 - Firebase saving auto generated id to it's collection when creating collection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM