如何在Mongoose中执行upsert查找嵌入式文档? [英] How to perform an upsert in Mongoose looking for an embedded document?

查看:88
本文介绍了如何在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,然后执行更新,似乎是一项艰巨的任务.可能吗?

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:

更新参数为:findQuery,data,queryOptions,onComplete

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);
});

这篇关于如何在Mongoose中执行upsert查找嵌入式文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆