如何在Mongoose中执行一个upsert查找嵌入式文档?
问题描述:
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)
这是我的模式。要检查SocialProfileSchema
内的特定user_id
,然后执行upsert看起来像是一项庞大的任务。它甚至有可能吗?如何在Mongoose中执行一个upsert查找嵌入式文档?
答
这里的,如果存在,你如何做一个更新的例子访问SocialProfiles直接而不是循环的,否则插入:
争鸣更新如下: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);
});
fyi你不需要使用callback.apply(...);你可以简单地使用回调(...)。 –