简体   繁体   English

Mongoose findOneAndUpdate 只返回新的 $push 文件

[英]Mongoose findOneAndUpdate return only the newly $push document

Here is an schema of my collection "profile":这是我的收藏“个人资料”的架构:

{ 

    _id : ObjectId("2bb0fad110"), 
    name : "Tommy", 
    defaultrates : 
    [ 
            {_id : ObjectId("444cbcfd52"), rate : 35.0, raisedOn : "5/2/2009"}, 
            {_id : ObjectId("2e642b1510"), rate : 55.0, raisedOn : "5/3/2010"}, 
            {_id : ObjectId("47cc4d69d3"), rate : 65.0, raisedOn : "5/5/2010"} 
    ] 
}

I want to $push an embedded array:我想 $push 一个嵌入式数组:

{
  _id : new ObjectID(), rate : 60.0, raisedOn : "7/5/2012"
}

and return this after execution.并在执行后返回。

So I made this thru mongoose,所以我通过猫鼬做了这个,

var condition = { '_id': "2bb0fad110"},
  insert = {
    '_id': new ObjectID(),
    'rate': 60.0,
    'raisedOn': "7/5/2012",

  },

  options = { strict: false, new: true };



function callback (err, result) {
  if (err) { console.log(err); res.status(400).send("error"); }
  console.log('result:' + JSON.stringify(result));
  res.json(result);
}


dbmodel.findOneAndUpdate(condition, { $push: { defaultrates: insert } }, options, callback);

It was good that the returned value (which is the whole object) is:返回值(即整个对象)是:

{

  _id : ObjectId("2bb0fad110"), 
  name : "Tommy", 
  defaultrates : 
  [ 
      {_id : ObjectId("444cbcfd52"), rate : 35.0, raisedOn : "5/2/2009"}, 
      {_id : ObjectId("2e642b1510"), rate : 55.0, raisedOn : "5/3/2010"}, 
      {_id : ObjectId("47cc4d69d3"), rate : 65.0, raisedOn : "5/5/2010"},
      {_id : ObjectId("65009be546"), rate : 60.0, raisedOn : "7/5/2012"}
  ] 
}

But my aim was to return only the value:但我的目标是只返回值:

{_id : ObjectId("65009be546"), rate : 60.0, raisedOn : "7/5/2012"}

and not the whole document.而不是整个文档。

How should I do it?我应该怎么做? What did i missed in this line:我在这一行中错过了什么:

dbmodel.findOneAndUpdate(condition, { $push: { defaultrates: insert } }, options, callback);

Seen as you are getting the full updated object back (due to the { new: true } option), you can locate the last element in the embedded document array using JavaScript and send that as your response.当您正在获取完整更新的对象时(由于{ new: true }选项),您可以使用 JavaScript 定位嵌入式文档数组中的最后一个元素并将其作为响应发送。

So with this object...所以有了这个对象...

{
  _id : ObjectId("2bb0fad110"), 
  name : "Tommy", 
  defaultrates : 
  [ 
      {_id : ObjectId("444cbcfd52"), rate : 35.0, raisedOn : "5/2/2009"}, 
      {_id : ObjectId("2e642b1510"), rate : 55.0, raisedOn : "5/3/2010"}, 
      {_id : ObjectId("47cc4d69d3"), rate : 65.0, raisedOn : "5/5/2010"},
      {_id : ObjectId("65009be546"), rate : 60.0, raisedOn : "7/5/2012"}
  ] 
}

You can do this...你可以这样做...

function callback (err, result) {
  if (err) { console.log(err); res.status(400).send("error"); }
  console.log('result:' + JSON.stringify(result));

  // BELOW ADDED
  const newDefaultRate = result.defaultrates[result.defaultrates.length - 1]; // gets the last element in the array
  res.status(201).json(newDefaultRate); // http status 201 to show new resource created
}

Hopefully this will help someone that comes across this thread.希望这会帮助遇到此线程的人。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM