如何在Mongoose中执行upsert寻找嵌入式文档?

时间:2011-09-22 16:15:31

标签: mongodb node.js mongoose

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: Math.round(new Date().getTime()/1000.0)

那是我的架构。要检查user_id中的特定SocialProfileSchema,然后执行upsert似乎是一项庞大的任务。它甚至可能吗?

2 个答案:

答案 0 :(得分:2)

以下是如何存在时如何进行更新的示例,否则插入:

更新的参数是: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);
});

答案 1 :(得分:-1)

dbref怎么样?它将允许您直接访问SocialProfiles,而不必循环遍历一堆嵌入的对象

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

相关问题