从最后一次出现一个阵列在MongoDB中更新对象 [英] MongoDb update object in a array on first occurence from last

查看:137
本文介绍了从最后一次出现一个阵列在MongoDB中更新对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个样本文档像图所示。

I have a sample document like shown below

{
 "_id" : "docID",
 "ARRAY" : [ 
    {
        "k" : "value",
        "T" : "20:15:35",
        "I" : "Hai"
    }, 
    {
        "K" : "some value",
        "T" : "20:16:35",
        "I" : "Hello"
    }, 
    {
        "K" : "some other value",
        "T" : "20:15:35",
        "I" : "Update"
    } 
   ]
}

我想在基于现场的数组更新的最后一个元素的ARRAY.T(这是唯一我知道更新的点域)中,但是我的问题是数组中的第一个元素相匹配的查询和它的 ARRAY.I 字段被更新。

I am trying to update the last element in the "ARRAY" based on field "ARRAY.T"(which is only field i know at the point of update), but what my problem is first element in the array matches the query and its ARRAY.I field is updated.

查询用于更新:

Query used to update:

db.collection.update( { _id: "docID","ARRAY.T" : "20:15:35"},
                { $set: { "ARRAY.$.I": "Updated value" }               
                })

其实我不知道数组的索引,其中更新,所以我不得不使用 ARRAY.I 在查询中,有没有什么办法来告诉MongoDB的更新的第一个元素匹配从去年数组的查询。

Actually i don't know index of the array where to update so i have to use ARRAY.I in the query, is there any way to to tell Mongodb to update the first element matched the query from last of the array.

推荐答案

我明白你说什么,你要到最后一个元素在这种情况下,其实还是过程中的匹配以相反的顺序相匹配。有没有办法修改这个并存储在位置指数 $ 运营商将永远是第一的比赛。

I understand what you are saying in that you want to match the last element in this case or in fact process the match in reverse order. There is no way to modify this and the index stored in the positional $ operator will always be the "first" match.

不过,您可以将您的办法改变这一点,作为 <默认行为STRONG> $推 是追加到数组的结尾。但是MongoDB的2.6推出了 $位置 修改,所以你可以在事实上总是pre-挂起到数组这意味着你的最老的产品在年底。

But you can change your approach to this, as the default behavior of $push is to "append" to the end of the array. But MongoDB 2.6 introduced a $position modifier so you can in fact always "pre-pend" to the array meaning your "oldest" item is at the end.

拿这个例如:

db.artest.update(
   { "array": { "$in": [5] } },
   { "$push": { "array": { "$each": [5], "$position": 0 } }},
   { "upsert": true }
)

db.artest.update(
    { "array": { "$in": [5] } },
    { "$push": { "array": { "$each": [6], "$position": 0 } }},
    { "upsert": true }
)

这导致了一份文件,是反向正常 $推在行为:

This results in a document that is the "reverse" of the normal $push behavior:

{ "_id" : ObjectId("53eaf4517d0dc314962c93f4"), "array" : [ 6, 5 ] }

另外,你可以应用 $排序 为了更新文档时修改以订单的元素,使他们被逆转。但是这可能不是最佳的选择,如果被存储的重复值

Alternately you could apply the $sort modifier when updating your documents in order to "order" the elements so they were reversed. But that may not be the best option if duplicate values are stored.

所以考虑存储反向如果你打算第一匹配最新项目的数组。目前,这是唯一的,从最后一场比赛的行为让你的方式。

So look into storing your arrays in "reverse" if you intend to match the "newest" items "first". Currently that is your only way of getting your "match from last" behavior.

这篇关于从最后一次出现一个阵列在MongoDB中更新对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆