$ and运算符在mongodb中不返回任何结果?

时间:2019-05-17 17:54:24

标签: node.js mongodb

大家好,我是MongoDB的初学者。我想在mongodb中返回一些文档。我正在使用NodeJS。我的集合中有多个文档。以我为例,我想返回具有名为make 2002和2008的列的文档,并且我只需要名称为return的列。谢谢。...

 My code:
    db.collection('cars').find( 
                {$and:[ {$in:{make:{2002,2008}} ]}
                ,{name:1,_id:0},(err,doc)=>{
                if(err){
                    throw err;
                }
                else{
                    console.log(doc);
                }});



                I have collections in my db:

                /* 1 */
    {
        "_id" : ObjectId("5cddda829719b0234ccef10d"),
        "name" : "Posche",
        "make" : 2002,
        "price" : 2300000
    }

    /* 2 */
    {
        "_id" : ObjectId("5cdee01d1f92122b785874ba"),
        "name" : "Audi",
        "make" : 2002,
        "price" : 2300000
    }

    /* 3 */
    {
        "_id" : ObjectId("5cdee01d1f92122b785874bb"),
        "name" : "BMW",
        "make" : 2003,
        "price" : 2000000
    }

    /* 4 */
    {
        "_id" : ObjectId("5cdee01d1f92122b785874bc"),
        "name" : "Toyoto",
        "make" : 2005,
        "price" : 1500000
    }

    /* 5 */
    {
        "_id" : ObjectId("5cdee3e71f0e0ab8a16cb2c8"),
        "make" : 2008,
        "name" : "Volvo",
        "price" : 59862
    }


    My Expected output:

    [{"name" : "Posche"},
     {"name" : "Audi"},
     {"name" : "volvo"}]

1 个答案:

答案 0 :(得分:2)

您似乎要查找的查询是:

carry = 0 ...其余代码

这将返回db.collection('cars').find({ make: {$in:[2002,2008]},{name:1,_id:0},与前两年匹配的文档。