Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to project whether field exists

Tags:

mongodb

If I have documents with similar structure as below. I am updating them with the results of the computations and I want to know whether the result has already been inserted into a document or not. Let's say for each document I run computation 'c' and computation 'd'. Now I want to display a table of all documents and show whether a computation 'd' has been already carried out. And for this table I do not care about computation 'c'.

{
"_id":1
"a":1,
"resultsOfComputation":{
   "c":{large embedded document},
   "d":{large embedded document}   
   }
}

{
"_id":2
"a":1,
"resultsOfComputation":{
   "c":{large embedded document}
   }
}

I would like to get a result that tells me whether a document contains a specific field. For example, I would like to know whether it contains field "resultsOfComputation.d", no matter what is the value of that field.

An example of the result of the query for "resultsOfComputation.d" would be:

{
"_id":1
"a":1,
"resultsOfComputation":{
   "d":true   
   }
}

{
"_id":2
"resultsOfComputation":{
   "d":false
   }
}

If "resultsOfComputation.d" is not in the document it can also be undefined, which is also ok:

{
"_id":1
"a":1,
"resultsOfComputation":{
   "d":true   
   }
}

{
"_id":2
"a":1,
"resultsOfComputation":{}
}

In general, the idea is to get all the root elements of the documents, but only true/false/undefined for the selected (one) result of computation, since the result of computation is a large embedded document.

like image 299
Marcel Avatar asked Aug 02 '16 12:08

Marcel


People also ask

How do you check if a field exist in MongoDB?

In MongoDB, we can check the existence of the field in the specified collection using the $exists operator. When the value of $exists operator is set to true, then this operator matches the document that contains the specified field(including the documents where the value of that field is null).

What does $project do in MongoDB?

The $project takes a document that can specify the inclusion of fields, the suppression of the _id field, the addition of new fields, and the resetting of the values of existing fields. Alternatively, you may specify the exclusion of fields. Specifies the inclusion of a field.

Which field is always included in a projection unless specifically excluded?

The _id field is included automatically unless specifically excluded.


1 Answers

Run the following aggregation pipeline to get the desired results:

db.collection.aggregate([
    {
        "$project": {
            "a": 1,
            "resultsOfComputation": {
                "d": { "$gt": ["$resultsOfComputation.d", null] }   
            }
        }
    }
])

Sample Output

/* 1 */
{
    "_id" : 1,
    "a" : 1,
    "resultsOfComputation" : {
        "d" : true
    }
}

/* 2 */
{
    "_id" : 2,
    "a" : 1,
    "resultsOfComputation" : {
        "d" : false
    }
}
like image 186
chridam Avatar answered Oct 13 '22 00:10

chridam