繁体   English   中英

如何给JavaScript中的MongoDB whereObj添加where条件

[英]How to add where condition to the MongoDB whereObj in JavaScript

我正在使用 MongoDB 处理 JavaScript。

我有集合名称 test_collection。 它的字段/对象为 test_field_1,其中包含 test_sub_field_1 和 test_sub_field_2。

现在,我把

var whereObj = {};

var cursor = collection.find(whereObj, {
            '_id': 0
    
        });   

我尝试使用 test_sub_field_1.= 34 但它失败了。 我正在尝试为 test_sub_field_1,= 34 和 test_sub_field_1.= 12 放置条件。在当前情况下,whereObj 为空。

var cursor = collection.find(whereObj, {
            '_id': 0,
            'test_field_1.test_sub_field_1': { $ne: 34 }
        });  

谢谢

MongoDB 中的find()需要 3 个输入:查询投影选项(按此顺序)。

您将whereObj作为查询输入(它是空对象)传递。 您应该像这样更改代码:

const whereObj = {
  'test_field_1.test_sub_field_1': { $ne: 34 }
};

const cursor = collection.find(whereObj);  

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM