mongodb数组重新排序和更新

时间:2022-10-19 09:39:57

I am using mongo for storing private messages, and for example i have an array like so:

我使用mongo存储私人消息,例如我有一个像这样的数组:

{
  "_id": { "$oid" : "520b842005ab0d0000000003" },
  "pmhistory": [
    {
      "sendername": "Tati",
      "preview": "Hey Man!",
    },
    {
      "sendername": "Dan",
      "preview": "Hey Moon!",
    },
    {
      "sendername": "Hellen",
      "preview": "Hello, Im proper!",
    },
  ],
  "user": { "$oid" : "520b842005ab0d0000000002" },
  "userid": "1"
}

If Dan sends a new message, I need to move his array entry to position 0 (pmhistory[0]) and update the preview field.

如果Dan发送新消息,我需要将其数组条目移动到位置0(pmhistory [0])并更新预览字段。

Now i am forced to issue two queries to the database, one with the $pull parameter, that takes out Dans entry from the pmhistory array and another with the $set parameter that adds Dans entry to the beginning of the pmhistory array.

现在我*向数据库发出两个查询,一个带有$ pull参数,它从pmhistory数组中取出Dans条目,另一个带有$ set参数,将Dans条目添加到pmhistory数组的开头。

Is it possible to use 1 query instead of two? Without getting have conflicting mods in update, because i am changing the size of the array.

是否可以使用1个查询而不是2个?没有在更新中有冲突的mods,因为我正在改变数组的大小。

2 个解决方案

#1


1  

I think the easiest would be to not rely on the array order in the first place. What I would do is add an additional field "last_update" to each subdocument, like:

我认为最简单的方法是首先不要依赖数组顺序。我要做的是为每个子文档添加一个额外的字段“last_update”,如:

{
  "_id": { "$oid" : "520b842005ab0d0000000003" },
  "pmhistory": [
    {
      "sendername": "Tati",
      "preview": "Hey Man!",
      "last_updated": 1376581408222,
    },
    {
      "sendername": "Dan",
      "preview": "Hey Moon!",
      "last_updated": 1376581406444,
    },
    {
      "sendername": "Hellen",
      "preview": "Hello, Im proper!",
      "last_updated": 1376581508111,
    },
  ],
  "user": { "$oid" : "520b842005ab0d0000000002" },
  "userid": "1"
}

Then, when Dan has a new message, you can just run a findAndModify like this:

然后,当Dan有一条新消息时,您可以像这样运行一个findAndModify:

db.so.findAndModify( { 
    query: { "pmhistory.sendername" : "Dan" }, 
    update: { 
        $set: { "pmhistory.$.last_updated" : (new Date).getTime() } 
    }, 
    new: true 
} );

Which then returns:

然后返回:

{
    "_id" : ObjectId("520cf82fd3541122f936f72e"),
    "pmhistory" : [
        {
            "sendername" : "Tati",
            "preview" : "Hey Man!",
            "last_updated" : 1376581408
        },
        {
            "sendername" : "Dan",
            "preview" : "Hey Moon!",
            "last_updated" : 1376581716876
        },
        {
            "sendername" : "Hellen",
            "preview" : "Hello, Im proper!",
            "last_updated" : 1376581508
        }
    ],
    "user" : ObjectId("520b842005ab0d0000000002"),
    "userid" : "1"
}

Then in your application, in the display part, just make sure your order by this new last_updated field descendingly.

然后在您的应用程序中,在显示部分中,只需确保您的新last_updated字段的订单递减。

#2


0  

You can use $push in conjunction with $slice to fix the array size and purge the last element.

您可以将$ push与$ slice结合使用来修复数组大小并清除最后一个元素。

http://docs.mongodb.org/manual/reference/operator/slice/

#1


1  

I think the easiest would be to not rely on the array order in the first place. What I would do is add an additional field "last_update" to each subdocument, like:

我认为最简单的方法是首先不要依赖数组顺序。我要做的是为每个子文档添加一个额外的字段“last_update”,如:

{
  "_id": { "$oid" : "520b842005ab0d0000000003" },
  "pmhistory": [
    {
      "sendername": "Tati",
      "preview": "Hey Man!",
      "last_updated": 1376581408222,
    },
    {
      "sendername": "Dan",
      "preview": "Hey Moon!",
      "last_updated": 1376581406444,
    },
    {
      "sendername": "Hellen",
      "preview": "Hello, Im proper!",
      "last_updated": 1376581508111,
    },
  ],
  "user": { "$oid" : "520b842005ab0d0000000002" },
  "userid": "1"
}

Then, when Dan has a new message, you can just run a findAndModify like this:

然后,当Dan有一条新消息时,您可以像这样运行一个findAndModify:

db.so.findAndModify( { 
    query: { "pmhistory.sendername" : "Dan" }, 
    update: { 
        $set: { "pmhistory.$.last_updated" : (new Date).getTime() } 
    }, 
    new: true 
} );

Which then returns:

然后返回:

{
    "_id" : ObjectId("520cf82fd3541122f936f72e"),
    "pmhistory" : [
        {
            "sendername" : "Tati",
            "preview" : "Hey Man!",
            "last_updated" : 1376581408
        },
        {
            "sendername" : "Dan",
            "preview" : "Hey Moon!",
            "last_updated" : 1376581716876
        },
        {
            "sendername" : "Hellen",
            "preview" : "Hello, Im proper!",
            "last_updated" : 1376581508
        }
    ],
    "user" : ObjectId("520b842005ab0d0000000002"),
    "userid" : "1"
}

Then in your application, in the display part, just make sure your order by this new last_updated field descendingly.

然后在您的应用程序中,在显示部分中,只需确保您的新last_updated字段的订单递减。

#2


0  

You can use $push in conjunction with $slice to fix the array size and purge the last element.

您可以将$ push与$ slice结合使用来修复数组大小并清除最后一个元素。

http://docs.mongodb.org/manual/reference/operator/slice/