Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何在Nodejs上使用Mongo中的$not操作符_Node.js_Mongodb - Fatal编程技术网

Node.js 如何在Nodejs上使用Mongo中的$not操作符

Node.js 如何在Nodejs上使用Mongo中的$not操作符,node.js,mongodb,Node.js,Mongodb,假设我有一个mongo db,里面有一堆这样的文档: { key: 'somekey', events: [ {event_id: '001', event_info: 'blah'}, {event_id: '001', event_info: 'blah'}, ...etc ] } 我想在key='X'所在文档的事件列表中添加一个新事件,但前提是该事件(由event_id标识)尚未存在 我试过这个: collectio

假设我有一个mongo db,里面有一堆这样的文档:

{
    key: 'somekey',
    events: [
        {event_id: '001', event_info: 'blah'},
        {event_id: '001', event_info: 'blah'},
        ...etc
    ]
}
我想在key='X'所在文档的事件列表中添加一个新事件,但前提是该事件(由event_id标识)尚未存在

我试过这个:

collection.update({
        'key': newKeyValue,
        $not { events.event_id: newEventId }
    }, 
    {
        '$push': { 'events': newEvent }
    }, 
    {
        'upsert': true
    });
但我得到了一个错误:“MongoError:未知的顶级操作符:$not”

我怎么能做这样的否定呢


另外请注意:我希望能够在一个原子操作中实现这一点。我知道我可以用多个查询来完成,但如果我以后想用批量操作来设置,那就行不通了。

您的查询是错误的,请尝试下面的查询

collection.update({
        'key': newKeyValue,
        events.event_id: { $not:{$eq: newEventId }}
    }, 
    {
        '$push': { 'events': newEvent }
    }, 
    {
        'upsert': true
    });
$not用于查询上述字段


希望这能解决您的问题

您的查询是错误的,请尝试下面的查询

collection.update({
        'key': newKeyValue,
        events.event_id: { $not:{$eq: newEventId }}
    }, 
    {
        '$push': { 'events': newEvent }
    }, 
    {
        'upsert': true
    });
$not用于查询上述字段


这可能会解决您的问题

您实际上想要
$ne
在这里处理此案例。另外,不要在匹配中使用否定与“upsert”组合。您可能应该执行两个操作。对于这种情况,您实际上需要
$ne
。另外,不要在匹配中使用否定与“upsert”组合。您可能应该执行两个操作。