prashantgpt91
prashantgpt91

Reputation: 1795

error in mongo DB query - $cond operator

This is BSON document. I want only those entries whose attribute length is greater than zero

/* 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"
    }

So output should be

       /* 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"
        }

What I tried

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

Upvotes: 1

Views: 43

Answers (1)

RootHacker
RootHacker

Reputation: 1107

This can be simply done using $gt Operator

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

Upvotes: 2

Related Questions