1

Asking for help to fix this mongoDb/mongoose query. It should do :

  • evaluate the quantity of array elements of 'host'

  • return only documents that have more than 2 elements into array

  • finally, the query should get back all documents/objects of DB with ALL FIELDS following the paraments above.

Sample Document :

{
    "_id" : ObjectId("XXXXXXXXX"),
    "content" : "ASdkJAHKDA",
    "time" : ISODate("2020-01-24T17:20:00.023Z"),
    "sentEmail" : {
        "host" : [ 
            "smtp.XXX", 
            "smtp.YYYY", 
        ],
        "status" : false,
        "date" : ISODate("2020-01-27T14:36:08.311Z",
}

Attempt 1 :

db.getCollection('emails').find({'sentEmail.status': false}, {$where:'sentEmail.host >= 2'})

Problem : It is not return ALL fields, it returns only ID

Attempt 2 :

db.getCollection('emails').find({'sentEmail.host.1': {$exists:true}, 'sentEmail.status': false})

Problem : It returns only the first object found.

2
  • Please translate it to English to get an answer!! Commented Jan 27, 2020 at 17:41
  • 1
    @srinivasy done Commented Jan 27, 2020 at 18:04

1 Answer 1

1

You can try this :

db.getCollection('emails').find({$expr :{$and : [{$eq :['$sentEmail.status', false]},
                                            {$gte: [{$size:'$sentEmail.host'},2]}]}})

Collection Data :

/* 1 */
{
    "_id" : ObjectId("5e2f26ead02e05b6948bc302"),
    "content" : "ASdkJAHKDA",
    "time" : ISODate("2020-01-24T17:20:00.023Z"),
    "sentEmail" : {
        "host" : [ 
            "smtp.XXX", 
            "smtp.YYYY"
        ],
        "status" : false,
        "date" : ISODate("2020-01-27T14:36:08.311Z")
    }
}

/* 2 */
{
    "_id" : ObjectId("5e2f26f1d02e05b6948bc3b5"),
    "content" : "ASdkJAHKDA",
    "time" : ISODate("2020-01-24T17:20:00.023Z"),
    "sentEmail" : {
        "host" : [ 
            "smtp.YYYY"
        ],
        "status" : false,
        "date" : ISODate("2020-01-27T14:36:08.311Z")
    }
}

/* 3 */
{
    "_id" : ObjectId("5e2f26ffd02e05b6948bc4c8"),
    "content" : "ASdkJAHKDA",
    "time" : ISODate("2020-01-24T17:20:00.023Z"),
    "sentEmail" : {
        "host" : [ 
            "smtp.XXX", 
            "smtp.YYYY", 
            "smtp.ZZZZ"
        ],
        "status" : false,
        "date" : ISODate("2020-01-27T14:36:08.311Z")
    }
}

/* 4 */
{
    "_id" : ObjectId("5e2f2bd2d02e05b6948c2c58"),
    "content" : "ASdkJAHKDA",
    "time" : ISODate("2020-01-24T17:20:00.023Z"),
    "sentEmail" : {
        "host" : [ 
            "smtp.XXX", 
            "smtp.YYYY", 
            "smtp.ZZZZ"
        ],
        "status" : true,
        "date" : ISODate("2020-01-27T14:36:08.311Z")
    }
}

Result :

/* 1 */
{
    "_id" : ObjectId("5e2f26ead02e05b6948bc302"),
    "content" : "ASdkJAHKDA",
    "time" : ISODate("2020-01-24T17:20:00.023Z"),
    "sentEmail" : {
        "host" : [ 
            "smtp.XXX", 
            "smtp.YYYY"
        ],
        "status" : false,
        "date" : ISODate("2020-01-27T14:36:08.311Z")
    }
}

/* 2 */
{
    "_id" : ObjectId("5e2f26ffd02e05b6948bc4c8"),
    "content" : "ASdkJAHKDA",
    "time" : ISODate("2020-01-24T17:20:00.023Z"),
    "sentEmail" : {
        "host" : [ 
            "smtp.XXX", 
            "smtp.YYYY", 
            "smtp.ZZZZ"
        ],
        "status" : false,
        "date" : ISODate("2020-01-27T14:36:08.311Z")
    }
}

If did not return document with "_id" : ObjectId("5e2f26f1d02e05b6948bc3b5") because size of sentEmail.host < 2 && "_id" : ObjectId("5e2f2bd2d02e05b6948c2c58") is not returned cause it has sentEmail.status: true.

Sign up to request clarification or add additional context in comments.

2 Comments

Great! but you missed the sentEmail.status : false. I need that condition too. Could you help with it?
Awsome! Tks a lot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.