mongo数据库查询中的错误 - $ cond运算符

时间:2017-01-07 10:39:07

标签: mongodb

这是BSON文件。我只想要那些属性length大于零的条目

/* 1 */
    {
        "_id" : "http://exhentai.org",
        "length" : 0,
        "value" : ""
    }

    /* 2 */
    {
        "_id" : "http://vuclip.com",
        "length" : 77,
        "value" : "vuclip scroll learn official weve launched close news team pressroom linkedin"
    }

    /* 3 */
    {
        "_id" : "http://hbogo.com",
        "length" : 174,
        "value" : "hbo hbo anywhere hbo enjoy instant unlimited access every episode every season best hbo shows movies comedy sports documentaries hbo free subscription participating providers"
    }

所以输出应该是

       /* 2 */
        {
            "_id" : "http://vuclip.com",
            "length" : 77,
            "value" : "vuclip scroll learn official weve launched close news team pressroom linkedin"
        }

        /* 3 */
        {
            "_id" : "http://hbogo.com",
            "length" : 174,
            "value" : "hbo hbo anywhere hbo enjoy instant unlimited access every episode every season best hbo shows movies comedy sports documentaries hbo free subscription participating providers"
        }

我尝试了什么

db.coll.aggregate([
    {$project: { 
       Text:
               {
                 $cond: { if: { $gte: [ "$length", 20 ] }, then: "$value" }
               }
       _id : 0
     }}
])

1 个答案:

答案 0 :(得分:2)

这可以使用$gt运算符

完成
db.collection.find({'length':{ $gt:0 }})