Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MongoDB通过值匹配更新数组元素_Mongodb_Mongodb Update - Fatal编程技术网

MongoDB通过值匹配更新数组元素

MongoDB通过值匹配更新数组元素,mongodb,mongodb-update,Mongodb,Mongodb Update,我有一个“地点”集合,我想用“美国+美国”替换“美国”。DB的当前状态如下: > db.locations.find({'country':'usa'}) { "_id" : "boston", "country" : [ "usa" ], "server" : "", "ts" : 1369642586.077319, "type" : [ "city" ] } { "_id" : "chicago", "country" : [ "usa" ], "server" : "", "ts"

我有一个“地点”集合,我想用“美国+美国”替换“美国”。DB的当前状态如下:

> db.locations.find({'country':'usa'})
{ "_id" : "boston", "country" : [ "usa" ], "server" : "", "ts" : 1369642586.077319, "type" : [ "city" ] }
{ "_id" : "chicago", "country" : [ "usa" ], "server" : "", "ts" : 1369642589.226037, "type" : [ "city" ] }
{ "_id" : "las+vegas", "country" : [ "usa" ], "server" : "", "ts" : 1369642591.688284, "type" : [ "city" ] }
{ "_id" : "los+angeles", "country" : [ "usa" ], "server" : "", "ts" : 1369642601.672113, "type" : [ "city" ] }
{ "_id" : "miami", "country" : [ "usa" ], "server" : "", "ts" : 1369642606.281971, "type" : [ "city" ] }
{ "_id" : "new+york", "country" : [ "usa" ], "server" : "", "ts" : 1369642611.884712, "type" : [ "city" ] }
{ "_id" : "portland", "country" : [ "usa" ], "server" : "", "ts" : 1369642615.59296, "type" : [ "city" ] }
{ "_id" : "san+francisco", "country" : [ "usa" ], "server" : "2", "ts" : 1369642619.255885, "type" : [ "city" ] }
{ "_id" : "san+jose", "country" : [ "usa" ], "server" : "", "ts" : 1369642622.74329, "type" : [ "city" ] }
{ "_id" : "seattle", "country" : [ "usa" ], "server" : "", "ts" : 1369642625.195549, "type" : [ "city" ] }
{ "_id" : "washington+dc", "country" : [ "usa" ], "server" : "", "ts" : 1369642628.37334, "type" : [ "city" ] }
{ "_id" : "cleveland", "country" : [ "usa" ], "server" : "2", "ts" : 1369642634.523065, "type" : [ "city" ] }
{ "_id" : "columbus", "country" : [ "usa" ], "server" : "2", "ts" : 1369642636.874618, "type" : [ "city" ] }
{ "_id" : "dallas", "country" : [ "usa" ], "server" : "2", "ts" : 1369642639.080141, "type" : [ "city" ] }
{ "_id" : "denver", "country" : [ "usa" ], "server" : "2", "ts" : 1369642642.236581, "type" : [ "city" ] }
{ "_id" : "houston", "country" : [ "usa" ], "server" : "2", "ts" : 1369642644.783804, "type" : [ "city" ] }
{ "_id" : "minneapolis", "country" : [ "usa" ], "server" : "2", "ts" : 1369642647.153817, "type" : [ "city" ] }
{ "_id" : "nashville", "country" : [ "usa" ], "server" : "2", "ts" : 1369642650.497276, "type" : [ "city" ] }
{ "_id" : "new+orleans", "country" : [ "usa" ], "server" : "2", "ts" : 1369642653.584663, "type" : [ "city" ] }
{ "_id" : "philadelphia", "country" : [ "usa" ], "server" : "2", "ts" : 1369642656.524054, "type" : [ "city" ] }
我运行了下面的MongoDB查询,这应该可以做到:

db.locations.update({'country':'usa'}, {'$set': {'country.$': 'united+states'}}, multi=true)

但是,这并没有达到预期效果,收集文档仍然使用“usa”而不是“usa+USES”。

不可能更新多个数组元素,请检查此答案,如果答案正确,这可能有效

db.locations.find().forEach(function(doc) { 
    do { 
       db.locations.update({'country' : 'usa'}, 
       { $set : {'country.$' : 'united states'}}, true); 
    } while(db.getPrevError().n != 0); 
});

您的更新呼叫中似乎有输入错误,请尝试以下操作:

db.locations.update({'country':'usa'}, {'$set': {'country.$': 'united+states'}}, {multi:true})

注意:选项现在是
{multi:true}
,而不是
multi=true
(或者只是
true

您是否进行了交叉检查。默认情况下,“更新”仅更新一个文档,而不是所有匹配的文档。您的更新可能有效-只需检查db.locations.count({'country':'usa'})在更新前后是否返回相同的值。