findAndModify与update更新的使用

yii框架  yii/mongo/Query 需要更新集合中特定的字段 ,Query里面查了查只有 mogify  折腾了半天,只是可以整个文档的替代 ,不信邪 ,跟进去看源码,原来是findAndModify 

参考文档:  https://docs.mongodb.com/manual/reference/method/db.collection.findAndModify/#db.collection.findAndModify

用法:

db.collection.findAndModify({
    query: ,
    sort: ,
    remove: ,
    update: ,
    new: ,
    fields: ,
    upsert: ,
    bypassDocumentValidation: ,
    writeConcern: ,
    collation: 
});

eg:

db.getCollection('cuishou_itag_push').update({sid:'5b6401b30df62e552ba2bf9e8f843cf0'},{$set:{itag_push_client_response_str:''}}, {multi:true});

db.getCollection('sid_info').findAndModify({query:{'sid':'SIDe7284ba6fd2c4146986d9c355d7f0e72'},
update:{$set:{'status_report':2,sid:'SIDe7284ba6fd2c4146986d9c355d7f0e72'}},
new:true,
fields:{'status_report':1},  
upsert:false
}


解释:

query:   where

update    要更新的项目, 有 $set的时候是只是更新选定的字符串  不然的话整个集合替换掉



你可能感兴趣的:(mongo,mongo)