简体   繁体   中英

mongodb update with $set and $upsert

I want to add a new document if not exists and update attributes if exists. as well as while updating a document, create attributes if not exists.

I tried the below way. but it works only updates.

      Fbuser= {};
      Fbuser.name = response.name;
      Fbuser.status = "active";
      Fbuser.profile_image = "https://graph.facebook.com/"+response.id+"/picture?width=150&height=150";

      db.Users.update({"email":response.email},{$set:Fbuser},{upsert:true})

$set needs always a value: http://docs.mongodb.org/manual/reference/operator/update/set/

Try this:

db.Users.update({"email":response.email},{$set:{Fbuser: null}}, {upsert:true})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM