Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.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.Collection.update()删除了我的文档_Mongodb_Robo3t - Fatal编程技术网

MongoDb.Collection.update()删除了我的文档

MongoDb.Collection.update()删除了我的文档,mongodb,robo3t,Mongodb,Robo3t,我在mongodb数据库上运行update语句,它删除了我的文档!我做错了什么 这是update语句: db.Question.update( {Name: "IsSomeCompany"}, {ButtonValues: [0, 1, 2] } ) 我使用的是Robomongo,回复是在22毫秒内更新了1条记录,但当我检查数据库时,记录消失了。我错过了什么 我使用此语法更新多个文档 db.getCollection('YourDocument').update( { "matching

我在
mongodb
数据库上运行
update
语句,它删除了我的文档!我做错了什么

这是update语句:

db.Question.update( {Name: "IsSomeCompany"}, {ButtonValues: [0, 1, 2] } )

我使用的是
Robomongo
,回复是
在22毫秒内更新了1条记录
,但当我检查数据库时,记录消失了。我错过了什么

我使用此语法更新多个文档

db.getCollection('YourDocument').update(
   { "matchingField" : "matchingValue"}, 
   { "$set": { "field": "value" } }, 
   { "multi": true } 
)

如果您试图只更新一个字段,则应使用$set运算符。 但是如果你想替换我和Mongo和Meteor有同样问题的文档。我在Mongo文档中发现,要替换文档,不需要使用$operator,只需传递字段/值对:

以下操作传递一个仅包含字段和值对的文档。除_id字段外,该文档将完全替换原始文档

但在Robomongo和Meteor方法中,这都删除了文件

我找到的唯一方法是使用$set操作符并替换每个字段。但这会尝试更新mongo'u id',所以我认为这只是一个解决办法

db.books.update({ item: "XYZ123" },
    {
      $set: {
        item: "XYZ123",
        stock: 10,
        info: { publisher: "2255", pages: 150 },
        tags: [ "baking", "cooking" ]
      }
    })

我错过了什么/

“唱片”是什么意思?它应该是文档字段还是整个文档?那么,您知道您的更新查询没有任何
$set
$unset
运算符吗?您将与
{Name:“IsSomeCompany”}
条件匹配的文档替换为
{buttonValue:[0,1,2]}
。如果您计划更新匹配文档的部分内容,请使用
$set
运算符。您使用的工具不支持;在这种情况下没关系。我使用这种语法来更新多个文档。db.getCollection('YourDocument').update({“matchingField”:“matchingValue”},{$set:{“field”:“value”},{multi:true})@Hamedz谢谢。你能写下这是一个答案吗?更新的文档不清楚(至少我在哪里看过),我没有看到您需要$set。所以我认为这可能是一个有价值的答案。
db.books.update({ item: "XYZ123" },
    {
      $set: {
        item: "XYZ123",
        stock: 10,
        info: { publisher: "2255", pages: 150 },
        tags: [ "baking", "cooking" ]
      }
    })