2017-07-07 56 views
-2

如何更新mongodb中的数据?如何更新mongodb中的数据?

这是我的我的收藏的实际结构

db.collection.find().pretty() 
{ 
    "_id" : ObjectId("557914833ac61e518e6103ab"), 
    "dataValue" : { 
     "value1" : [ 
      "value1 A", 
      "value1 B" 
     ] 
    } 
} 

我想在dataValue插入像默认

db.collection.find().pretty() 
{ 
    "_id" : ObjectId("557914833ac61e518e6103ab"), 
    "dataValue" : { 
     "value1" : [ 
      "value1 A", 
      "value1 B" 
     ], 
     "default" : [ 
      "default A", 
      "default B" 
     ] 
    } 
} 

请帮我

+0

https://docs.mongodb.com/manual/reference/method/db.collection.update/ – Alex

+1

[将新字段添加到MongoDB中的集合](https://stackoverflow.com/ questionor/7714216/add-new-field-to-a-collection-in-mongodb) – felix

回答

1

试试这个:

db.collection.update(
    { _id: ObjectId("557914833ac61e518e6103ab") }, //update doc with this id 
    { $set: 
     { 
     "dataValue.default": [ 
     "default A", 
     "default B" 
     ] 
     } 
    } 
) 

As按我的意见:https://docs.mongodb.com/manual/reference/method/db.collection.update/更新

语法是(我们只使用<query><update>部分的这种

db.collection.update(
    <query>, 
    <update>, 
    { 
    upsert: <boolean>, 
    multi: <boolean>, 
    writeConcern: <document>, 
    collation: <document> 
    } 
) 

在你的情况,你需要使用$set

dataValue.default使用点符号'到达'子文件中,并设置属性

+0

db.collection.find({dataValue.value1})。pretty()这一个不起作用 –

+0

这不是正确的查询语法 – Alex

+0

can yu给出dataValue.value1的正确语法 –

0

这可以使用$ set操作符简单更新来实现

db.collection.update({"_id" : ObjectId("557914833ac61e518e6103ab")}, 
{ $set: {"dataValue.default" : [ 
      "default A", 
      "default B" 
     ]}}) 
+0

db.collection.find({dataValue.value1})。pretty()this one not working –