2

I want to replace some document entirely with multi:true option.

db.user.find().pretty();
    {
        "_id" : ObjectId("5799b106d15203df993935d9"),
        "name" : "zhangyun",
        "age" : 33
    }
    {
        "_id" : ObjectId("5799b4d9e21ce60703d1e1c7"),
        "name" : "zhangli",
        "age" : 12
    }
    {
        "_id" : ObjectId("5799b5afe21ce60703d1e1c8"),
        "name" : "zhangyun",
        "height" : 1.75
    }

I want to do update operation.like this:

db.user.update({name:"zhangyun"},{sex:"male"},{multi:true})

I got "multi update only works with $ operators" error

the mongodb official document

https://docs.mongodb.com/manual/reference/method/db.collection.update/#multi-parameter just tell me it cannot,but no why and how to do in such cases.

can anybody help me with this problem ?

MrJ
  • 33
  • 1
  • 7
  • Notice:i want to replace entirely ,not to add the sex field – MrJ Jul 28 '16 at 08:21
  • it's not duplicate question with the question you marked.@chridam – MrJ Jul 28 '16 at 08:55
  • 1
    The error says it all `"multi update only works with $ operators"`, the answer in the marked duplicate addresses that, you need the `$set` operator even if you want to replace the entire document, not just the sex field. – chridam Jul 28 '16 at 09:26
  • i want to update and get result like this: ‘{ "_id" : ObjectId("5799b106d15203df993935d9"), "sex" : "male" } { "_id" : ObjectId("5799b4d9e21ce60703d1e1c7"), "name" : "zhangli", "age" : 12 } { "_id" : ObjectId("5799b5afe21ce60703d1e1c8"), "sex" : "male" } ’ so how to do? – MrJ Jul 28 '16 at 09:48
  • Maybe it's not meaningful operation.that is,we won't do this because if we do so,then the collection exists two document same with each other except the _id field. – MrJ Jul 28 '16 at 10:03

0 Answers0