Advertisement
Guest User

MongoDB Save

a guest
Dec 14th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. > db.myItems.save
  2. function (obj, opts) {
  3.     if (obj == null)
  4.         throw Error("can't save a null");
  5.  
  6.     if (typeof(obj) == "number" || typeof(obj) == "string")
  7.         throw Error("can't save a number or string");
  8.  
  9.     if (typeof(obj._id) == "undefined") {
  10.         obj._id = new ObjectId();
  11.         return this.insert(obj, opts);
  12.     } else {
  13.         return this.update({_id: obj._id}, obj, Object.merge({upsert: true}, opts));
  14.     }
  15. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement