1

我在使用 NEDBfind()查询结构时遇到问题。

下面的find()查询应该返回所有新消息:发送到指定的组email或指定的All组,并且指定的消息尚未读取或发送email

但是,它返回的是误报。例如,在结果中返回平面文件(如下)中的两项,尽管正在评估的属性数组中的email IS。我正在使用$nin操作员,但我无法弄清楚我做错了什么?欢迎所有建议:)

var email = 'anotheraddress@gmail.com';
var message_query = [];
    message_query.push({$and: [{to: 'All'}, {'data.readBy': {$nin: [email]}}]});
    message_query.push({$and: [{to: email}, {'data.read': {$exists: false}}]});

    message
        .find({
            $and: [
                {$not: {from: email}},
                {$or: message_query}
            ]
        })
        .exec(function (err, results) {

        });

这是平面文件的一个片段。这两个不应返回,但它们是:/

...

{ 
  to: 'All',
  toname: 'Some Name',
  from: 'someaddress@gmail.com',
  fromname: 'Some other Name',
  message: 'A message for all...',
  timestamp: 1502473320,
  imtype: 'msg',
  _id: 'K66iP3dZHbYTfGgK',
  data:
   { readBy:
      [ 'someotheraddress@gmail.com',
        'anotheraddress@gmail.com' ] } 
},
{ 
  to: 'All',
  toname: 'Some Name',
  from: 'someoneelse@gmail.com',
  fromname: 'Some other Name',
  message: 'A message for all...',
  timestamp: 1502473420,
  imtype: 'msg',
  _id: 'K66iP3dZNyNxfGgK',
  data:
   { readBy:
      [ 'someotheraddress@gmail.com',
        'anotheraddress@gmail.com' ] } 
}

...

提前致谢

4

1 回答 1

1

问题在于$nin操作员 - 它不像在 mongo 中那样工作。

NEDB 的$in操作符工作方式不同:只有一个操作数可以是数组。查看$in(毕竟$nin只是对的否定)的定义。$in当它检查数组的事物是否相等时,它确保只有一项是数组,即既不是数组也不ab[i]数组。如果是,则返回 false。$nin因此返回 true - 解释结果中两个文档的存在。

您可以按如下方式验证这一点:尝试更改[email]为类似的内容["someotheraddress@gmail.com", "anotheraddress@gmail.com", "foo@bar.com"],您会注意到两个结果消失了 - 证明它不会检查每个元素data.readBy是否不在我们提供的列表中,而是检查整个元素data.readBy是否存在我们提供的清单。

解决方案 使用{ $not: { elemMatch }

您可以通过将查询更改为以下内容来解决该问题:

message_query.push({
  $and: [{ to: "All" }, { $not: { "data.readBy": { $elemMatch: email } } }]
});

这是要验证的代码:

const Datastore = require("nedb");

const db = new Datastore({
  inMemoryOnly: true,
  autoload: false
});

db.insert(
  [
    {
      to: "All",
      toname: "Some Name",
      from: "someaddress@gmail.com",
      fromname: "Some other Name",
      message: "A message for all...",
      timestamp: 1502473320,
      imtype: "msg",
      data: {
        readBy: ["someotheraddress@gmail.com", "anotheraddress@gmail.com"]
      }
    },
    {
      to: "All",
      toname: "Some Name",
      from: "someoneelse@gmail.com",
      fromname: "Some other Name",
      message: "A message for all...",
      timestamp: 1502473420,
      imtype: "msg",
      data: {
        readBy: ["someotheraddress@gmail.com", "anotheraddress@gmail.com"]
      }
    },
    {
      to: "All",
      toname: "Some Name",
      from: "someoneelse@gmail.com",
      fromname: "Some other Name",
      message: "A message for all...",
      timestamp: 1502473420,
      imtype: "msg",
      data: {
        readBy: ["someotheraddress@gmail.com" ]
      }
    },
    {
      to: "foo@bar.com",
      toname: "Some Name",
      from: "someoneelse@gmail.com",
      fromname: "Some other Name",
      message: "A message for all...",
      timestamp: 1502473420,
      imtype: "msg",
      data: {
        readBy: ["someotheraddress@gmail.com", "anotheraddress@gmail.com"]
      }
    },
    {
      to: "anotheraddress@gmail.com",
      toname: "Some Name",
      from: "someoneelse@gmail.com",
      fromname: "Some other Name",
      message: "A message for all...",
      timestamp: 1502473420,
      imtype: "msg",
      data: {
        read: true
      }
    },
    {
      to: "anotheraddress@gmail.com",
      toname: "Some Name",
      from: "someoneelse@gmail.com",
      fromname: "Some other Name",
      message: "A message for all...",
      timestamp: 1502473420,
      imtype: "msg",
      data: {
      }
    }
  ],
  (...args) => {
    var email = "anotheraddress@gmail.com";

    var _list = ["someotheraddress@gmail.com", "anotheraddress@gmail.com", "foo@bar.com"];

    var message_query = [];

    message_query.push({
      $and: [{ to: "All" }, { $not: { "data.readBy": { $elemMatch: email } } }]
    });

    // message_query.push({
    //   $and: [{ to: "All" }, { "data.readBy": { $nin: [email] } }]
    // });

    // message_query.push({
    //   $and: [{ to: "All" }, { "data.readBy": { $nin: _list } }]
    // });

    message_query.push({
      $and: [{ to: email }, { "data.read": { $exists: false } }]
    });

    db.find({
      $and: [ { $not: { from: email } }, { $or: message_query } ]
    }).exec(function(err, results) {
      console.log(JSON.stringify(results, null, 2));
    });

  }
);
于 2017-08-14T06:13:56.087 回答