简体   繁体   English

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

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

I am working on JavaScript with MongoDB.我正在使用 MongoDB 处理 JavaScript。

I have collection name test_collection.我有集合名称 test_collection。 It has field/object as test_field_1, which contains test_sub_field_1 and test_sub_field_2.它的字段/对象为 test_field_1,其中包含 test_sub_field_1 和 test_sub_field_2。

Now, I am putting现在,我把

var whereObj = {};

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

I tried to use test_sub_field_1.= 34 but it failed.我尝试使用 test_sub_field_1.= 34 但它失败了。 I am trying to put where condition for test_sub_field_1,= 34 and test_sub_field_1.= 12. In current situation, whereObj is Empty.我正在尝试为 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 }
        });  

Thanks谢谢

find() in MongoDB takes 3 inputs: Query , Projection and Options (in that order). MongoDB 中的find()需要 3 个输入:查询投影选项(按此顺序)。

You are passing whereObj as a Query input (which is empty object).您将whereObj作为查询输入(它是空对象)传递。 You should change your code like this:您应该像这样更改代码:

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