开发者

How to perform an upsert in Mongoose looking for an embedded document?

SocialProfileSchema = new mongoose.Schema
  source:
    type: String
    enum: ['twitter','facebook']
    lowercase: true
  user_id: String
  profile_url: String
  primary:
    type: Boolean
    default: true
  added_on:
    type: String
    default: Math.round(new Date().getTime()/1000.0)

UserProfileSchema  = new mongoose.Schema
    socialProfiles: [SocialProfileSchema]
    added_on:
      type: String
      default: 开发者_C百科Math.round(new Date().getTime()/1000.0)

That's my Schema. To check for a specific user_id within a SocialProfileSchema and then perform an upsert seems like a gargantuan task. Is it even possible?


Here's an example of how you can do an update if exists, otherwise insert:

Arguments for update are: findQuery, data, queryOptions, onComplete

var update = { data: "1", expires: 300 };
that.update({ session_id: sid }, { $set: update }, { upsert: true }, function(err, data) {
  callback.apply(this, arguments);
});


How about a dbref? It will let you access the SocialProfiles directly instead of having to loop through a bunch of embedded objects

http://mongoosejs.com/docs/populate.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜