簡體   English   中英

一對多貓鼬-不太清楚如何實施

[英]Mongoose one-to-many - not quite sure how to implement it

我是貓鼬的新手(工作2天),我想建立一對多的關系,因為一個人可以來自一個國家,一個國家有很多人。

所以,這就是我所擁有的:

    var userSchema = new Schema({
    name: String,
    username: {
        type: String,
        required: true,
        unique: true
    },
    password: {
        type: String,
        required: true
    },  
    country: {
        type: Schema.Types.ObjectId,
        ref: 'Country'
    }

});
var User = mongoose.model('Person', userSchema);

var countrySchema = new Schema({
    name: {
        type: String,
        required: true,
        unique: true
    },
    created_at: Date,
    updated_at: Date,
    people: [{
        type: Number,
        ref: 'User'
    }]
});

var Country = mongoose.model('Country', countrySchema);

var UK = new Country({
    name: 'UK'
});


usa.save(function(err) {
    var user = new User({
        username: 'James',
        password: 'Bond',
        country: UK._id
    });

    user.save(function(err) {

    });

});

現在我有兩個問題:1)我已經看到ref有時可以是一個ObjectId或僅僅是一個數字-有什么區別? 2)在保存數據時,以我為例,我將國家/地區保存到一個人(通過_id),如何將一個人保存到一個國家? 我應該更新模型的實例嗎?

謝謝

更新:

由於此問題已被標記為重復,因此讓我改一下這個問題:考慮此鏈接中的官方示例: http : //mongoosejs.com/docs/populate.html這個想法是一個人有很多故事,一個故事有一位作者(人)。 因此,節省如下:

    var aaron = new Person({ _id: 0, name: 'Aaron', age: 100 });

aaron.save(function (err) {
  if (err) return handleError(err);

  var story1 = new Story({
    title: "Once upon a timex.",
    _creator: aaron._id    // assign the _id from the person
  });

  story1.save(function (err) {
    if (err) return handleError(err);
    // thats it!
  });
}); 

這是從官方文檔-我的問題是,在這里還是我們如何保存story1Author Author是在Story之前創建的,因此不應該使用story1._id更新Author

更新2:

我發現,如果僅使用type: Schema.Types.ObjectId而從不type: Number ,那么我可以做到這一點:

    var aaron = new Person({ _id: 0, name: 'Aaron', age: 100 });
var story1 = new Story({
    title: "Once upon a timex.",
    _creator: aaron._id    // assign the _id from the person
});
aaron.stories.push(story1._id);

aaron.save(function (err) {
  if (err) return handleError(err);
}); 
story1.save(function (err) {
    if (err) return handleError(err);
    // thats it!
}); 

這實際上在一個虛擬示例中有效...如果請求中的帖子過多而ID可能丟失/重復,是否會出現問題? 這種方法的缺點是什么?

1)我已經看到ref有時可以是一個ObjectId或只是一個數字-有什么區別?

請參考此問題, 為什么他們在貓鼬人口示例中使用ObjectId和Number?

我們在哪里或如何將Story1保存到作者

aaron.save(function (err) {
  if (err) return handleError(err);

  var story1 = new Story({
    title: "Once upon a timex.",
    _creator: aaron._id    // assign the _id from the person
  });

  story1.save(function (err) {
    if (err) return handleError(err);
    // save id of story1 into person here, also you should use `update` operation with `$push` operator.
    aaron.stories.push(story1._id);
    aaron.save(function(err){
        if (err)
            handleError(err);
        else
            console.log('save person successfully...');
    })
  });
});

結果

> db.stories.find()
{ "_id" : ObjectId("56f72f633cf1e6f00159d5e7"), "title" : "Once upon a timex.", "_creator" : 0, "fans" : [ ], "__v" : 0 }
> db.people.find()
{ "_id" : 0, "name" : "Aaron", "age" : 100, "stories" : [ ObjectId("56f72f633cf1e6f00159d5e7") ], "__v" : 1 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM