【问题标题】:mongoose/mongodb querying for records without a related recordmongoose/mongodb 查询没有相关记录的记录
【发布时间】:2014-06-10 15:08:40
【问题描述】:

我有两个猫鼬模型。我们称第一个模型为 foo,第二个模型为 bar。 Bar 记录有一个相关的 foo 记录和一个电子邮件地址。我希望我的 api 被传递一个电子邮件地址并返回一个没有使用该电子邮件地址创建的 bar 记录的 foo 记录列表。我将如何使用 mongoose 进行此操作?

我知道我可以用 SQL 编写这个查询,但我一直在尝试学习无 sql 数据库,因此是 mongo。

这是一个例子。我有 2 条 foo 记录和 2 条 bar 记录:

福斯:

{name:"first foo"}
{name:"second foo"}

还有我的酒吧记录:

{
    email:"requestEmail@example.com,
    foo_id:first_foo._id
}

{
    email:"someOther@example.com,
    foo_id:second_foo._id
}

对我的 api 的请求将通过电子邮件发送:requestEmail@example.com。在这种情况下,我想返回第二个 foo (以及任何其他 foo 记录),因为第一个 foo 有一个 bar 记录,其中包含请求中的电子邮件。

【问题讨论】:

  • 能否将生成的文档或记录粘贴到 mongo.你的文档在 MongoDB 中的样子
  • Aleksandar 的现有答案看起来是正确的。是什么促使您添加赏金?如果他的回答有什么具体的地方不起作用,如果您对此添加评论会很有帮助。
  • @pka2012 由于这里没有太多细节,我在做一些假设。在我的选择中,您应该更改架构以便链接两个模型。

标签: node.js mongodb mongoose


【解决方案1】:

这可能是最简单的两遍。首先,您应该检索所有 Bar 对象,然后根据它们过滤 Foo 对象。我没有 node.js 编译器,所以我的代码包含一些错误(我可以在当天晚些时候编辑它,但你会得到图片)。

var findFooWOutBar = function(theEmail)
{
  Bar.find({email: theEmail}, function(err,docs)
  {
    if(err)
    {
      console.log(err);
      return
    }
    else
    {
      var barIds=[];
      docs.forEach(function(bar) //fetching all of the bars with the email
      {
        barIds.push(bar._id);//or whatever you are using as a reference 
      });

      //nin means not in
      Foo.find().nin('barRef', barIds).exec(function(err,foos)
      {
        //process your Foo results (foos) here
      }); //have not tested this, but according to the docs it should go something like this      
    }
  });

}

所以基本上,这里可能有些东西不完全正确,但是您需要一个 Bar id 数组(或您正在使用的其他参考键)并将其与 nin 的使用(不是 in)结合起来。

【讨论】:

    【解决方案2】:

    我认为您应该先更改架构。 bar schema 可以定义如下:

    var Schema = require('mongoose').Schema;
    var barSchema = new Schema({
        email: {
            type: String,
            unique: true
        },
        fooId: {
        type: Schema.Types.ObjectId
        },
    });
    

    现在,fooSchema 可以定义如下:

    var Schema = require('mongoose').Schema;
    var fooSchema = new Schema({ 
        name: {
           type : String
        }   
    });
    

    好的,我们已经有了我们的架构。现在我们可以定义模型并按照自己的方式解决问题。

    var model = require('mongoose').model;
    var foo = model('Foo', fooSchema);
    var bar = model('Bar', barSchema);
    
    function fooWithNoBar(email) {
        var conditions = {
            email: email
        }
        bar.find(conditions, function (err, data) {
            if (err) {
                console.log(err);
                return
            } else {
                var barIds = [];
                data.forEach(function (bar) {
                    barIds.push(bar._id); 
                });
                conditions = {
                        _id: {
                            $nin: barIds
                        }
                    }
                foo.find(conditions, function (err, data) {
                    console.log("foo records that do not have a bar record created with that email address: ", data);
                });
            }
        });
    }
    

    注意:我从 Aleksandar 的回答中复制了一些代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-18
      • 1970-01-01
      • 1970-01-01
      • 2013-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-26
      相关资源
      最近更新 更多