在填充猫鼬后查找 [英] Find after populate mongoose

查看:77
本文介绍了在填充猫鼬后查找的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用猫鼬填充后,在查询文档内部匹配的值时遇到麻烦.

我的模式如下:

var EmailSchema = new mongoose.Schema({
  type: String
});

var UserSchema = new mongoose.Schema({
  name: String,
  email: [{type:Schema.Types.ObjectId, ref:'Email'}]
});

例如,我希望所有拥有电子邮件类型为"Gmail"的用户.

以下查询返回空结果:

Users.find({'email.type':'Gmail').populate('email').exec( function(err, users)
    {
      res.json(users);
    });

我不得不像这样在JS中过滤结果:

users = users.filter(function(user)
        {
          for (var index = 0; index < user.email.length; index++) {
            var email = user.email[index];
            if(email.type === "Gmail")
            {
              return true;
            }
          }
          return false;
        });

有没有办法直接从猫鼬那里查询类似的东西?

解决方案

@Jason Cust 已经很好地解释了-在这种情况下,最好的解决方案通常是更改架构以防止查询根据存储在单独集合中的文档的属性.

这是我能想到的最好的解决方案,但是不会强迫您这样做(因为您在评论中说不能).

Users.find().populate({
  path: 'email',
  match: {
    type: 'Gmail'
  }
}).exec(function(err, users) {
  users = users.filter(function(user) {
    return user.email; // return only users with email matching 'type: "Gmail"' query
  });
});

我们在这里所做的只是填充匹配其他查询的email(在.populate()调用中的match选项)-否则,在Users文档中的email字段将被设置为null. /p>

剩下的就是返回的users数组上的.filter,就像您最初的问题一样-仅使用更简单,非常通用的检查.如您所见-email存在或不存在.

I'm having some trouble querying a document by values matching inside the document after population by mongoose.

My schemas are something like this:

var EmailSchema = new mongoose.Schema({
  type: String
});

var UserSchema = new mongoose.Schema({
  name: String,
  email: [{type:Schema.Types.ObjectId, ref:'Email'}]
});

I would like to have all users which have a email with the type = "Gmail" for example.

The following query returns empty results:

Users.find({'email.type':'Gmail').populate('email').exec( function(err, users)
    {
      res.json(users);
    });

I have had to resort to filtering the results in JS like this:

users = users.filter(function(user)
        {
          for (var index = 0; index < user.email.length; index++) {
            var email = user.email[index];
            if(email.type === "Gmail")
            {
              return true;
            }
          }
          return false;
        });

Is there any way to query something like this straight from mongoose?

解决方案

@Jason Cust explained it pretty well already - in this situation often the best solution is to alter the schema to prevent querying Users by properties of documents stored in separate collection.

Here's the best solution I can think of that will not force you to do that, though (because you said in the comment that you can't).

Users.find().populate({
  path: 'email',
  match: {
    type: 'Gmail'
  }
}).exec(function(err, users) {
  users = users.filter(function(user) {
    return user.email; // return only users with email matching 'type: "Gmail"' query
  });
});

What we're doing here is populating only emails matching additional query (match option in .populate() call) - otherwise email field in Users documents will be set to null.

All that's left is .filter on returned users array, like in your original question - only with much simpler, very generic check. As you can see - either the email is there or it isn't.

这篇关于在填充猫鼬后查找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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