2017-12-26 1 views
0

mongodb에서 여러 레코드를 업데이트하려고합니다. 다음과 같이 하나의 기록은 다음과 같습니다몽고 업데이트 쿼리가 mysql에 해당

"_id" : ObjectId("59704fac4ed2aa47e68c7f50"), 
    "uid" : 157140, 
    "ts" : NumberLong("170720120636"), 
    "a" : { 
     "em" : "[email protected]", 
     "fk" : "[email protected]", 
     "mo" : "1248849893", 
     "d" : 0, 
     "bl" : 0, 
     "att3" : "city20788", 
     "att66" : "fname20788" 
    } 
} 
{ 
    "_id" : ObjectId("59704fac4ed2aa47e68c7f51"), 
    "uid" : 157141, 
    "ts" : NumberLong("170720120636"), 
    "a" : { 
     "em" : "[email protected]", 
     "fk" : "[email protected]", 
     "mo" : "1248849920", 
     "d" : 0, 
     "bl" : 0, 
     "att3" : "city20789", 
     "att66" : "fname20789" 
    } 
} 

내가 특정 레코드에 대한 배열 "a.d"의 값을 업데이트하고 싶었다.

db.userDetails.find({ uid : {$in : [157140,157141]}}).update({$set: {"a.d":1}}) 

하지만 난 오류가 점점 오전 :

쿼리 내가 노력하고있는

"errmsg" : "A single update on a sharded collection must contain an exact match on _id (and have the collection default collation) or contain the shard key (and have the simple collation). Update request: { q: { uid: { $in: [ 157140.0, 157141.0 ] } }, u: { $set: { a.d: 1.0 } }, multi: false, upsert: false }, shard key pattern: { uid: 1 }" 
+0

확인이 https://stackoverflow.com/questions/18173482/mongodb-update -deeply-nested-subdocument – Aman

답변

0
db.collection.find().forEach(function(doc) { 
do { 
    db.collection.update({_id: doc._id,{$elemMatch_{user__test_,{$ne:"new_avatar.jpg"}}}},{$set:{"comments.$.avatar":"new_avatar.jpg"}}); 
} while (db.getPrevError().n != 0); 
})