从另一条记录中检索其ID后,在Mongo中更新记录

时间:2021-08-15 04:20:31

I am trying to make an API point that would do the following. I submit an Object ID in the path. The record with that ID is found. Then, the program looks into a certain field of this object. The field contains an ObjectID for another entry in the database. At last, I need to pull up that record and increment a certain field in it.

我正在尝试创建一个可以执行以下操作的API点。我在路径中提交了一个对象ID。找到具有该ID的记录。然后,程序查看该对象的某个字段。该字段包含数据库中另一个条目的ObjectID。最后,我需要拉出该记录并增加其中的某个字段。

In short, I have a child->parent relationship between certain records and would like the ability of incrementing a certain field within the parent record by submitting the child's id to the API point.

简而言之,我在某些记录之间存在子 - >父关系,并希望通过将子ID提交到API点来增加父记录中的某个字段。

Here is the code I had that did the basic child increment. How can I go about doing it for the parent?

这是我做的基本子增量代码。我怎样才能为父母做这件事?

router.get('/today/parent/up/:id', function(req, res){
        var collection = db.get('Activity');
        collection.update({
            _id: req.params.id
        },
        {
            $inc: {
             "repetitions.today": 1,
             "repetitions.total": 1 
            }
        }, function(err, activity){
            if (err) throw err;
            res.json(activity);
        });
})

1 个解决方案

#1


1  

First use mongo references, heres documenttion: https://docs.mongodb.com/manual/reference/database-references/

首先使用mongo引用,下面是文档:https://docs.mongodb.com/manual/reference/database-references/

here's mongoose documentation http://mongoosejs.com/docs/2.7.x/docs/populate.html

这里是mongoose文档http://mongoosejs.com/docs/2.7.x/docs/populate.html

Basically You need to do this:

基本上你需要这样做:

var mongoose = require('mongoose')
, Schema = mongoose.Schema

var PersonSchema = new Schema({
    name    : String
, age     : Number
, stories : [{ type: Schema.ObjectId, ref: 'Story' }]
});

var StorySchema = new Schema({
    _creator : { type: Schema.ObjectId, ref: 'Person' }
, title    : String
, fans     : [{ type: Schema.ObjectId, ref: 'Person' }]
});

var Story  = mongoose.model('Story', StorySchema);
var Person = mongoose.model('Person', PersonSchema);

Then you could use .populate() method, and then you could extract your populated model and make changes and save them with .save(), but remember to use it in populated model, not the parent one. For ex. You've got author which contains reference to books, so you make request

然后你可以使用.populate()方法,然后你可以提取你的填充模型并进行更改并用.save()保存它们,但是记得在填充的模型中使用它,而不是父模型。对于前者您有作者,其中包含对书籍的引用,因此您提出请求

author.findOne({'name': 'King'}).populate('books').exec((err, king) => {
  let book0 = king.books[0];
  book0.title = 'I need to change this one';
  book0.save((err, data) => {
    console.log('saved referenced object')
  }
})

#1


1  

First use mongo references, heres documenttion: https://docs.mongodb.com/manual/reference/database-references/

首先使用mongo引用,下面是文档:https://docs.mongodb.com/manual/reference/database-references/

here's mongoose documentation http://mongoosejs.com/docs/2.7.x/docs/populate.html

这里是mongoose文档http://mongoosejs.com/docs/2.7.x/docs/populate.html

Basically You need to do this:

基本上你需要这样做:

var mongoose = require('mongoose')
, Schema = mongoose.Schema

var PersonSchema = new Schema({
    name    : String
, age     : Number
, stories : [{ type: Schema.ObjectId, ref: 'Story' }]
});

var StorySchema = new Schema({
    _creator : { type: Schema.ObjectId, ref: 'Person' }
, title    : String
, fans     : [{ type: Schema.ObjectId, ref: 'Person' }]
});

var Story  = mongoose.model('Story', StorySchema);
var Person = mongoose.model('Person', PersonSchema);

Then you could use .populate() method, and then you could extract your populated model and make changes and save them with .save(), but remember to use it in populated model, not the parent one. For ex. You've got author which contains reference to books, so you make request

然后你可以使用.populate()方法,然后你可以提取你的填充模型并进行更改并用.save()保存它们,但是记得在填充的模型中使用它,而不是父模型。对于前者您有作者,其中包含对书籍的引用,因此您提出请求

author.findOne({'name': 'King'}).populate('books').exec((err, king) => {
  let book0 = king.books[0];
  book0.title = 'I need to change this one';
  book0.save((err, data) => {
    console.log('saved referenced object')
  }
})