MongoDb exclude null in aggregation $project -


i have data collection in mongodb shape:

[{ "_id": "1",   "sq1" : 5,    "sq1comment" : "in general aaaaaa.",    "sq2" : 8,    "s2comment" : null,    "sq3" : 5,    "sq3comment" : "a person bbbbb."  },  { "_id": "2",    "sq1" : 4,     "sq1comment" : "in general cc.",     "sq2" : 8,     "s2comment" : "a story ff",     "sq3" : 5,     "sq3comment" : null  } ] 

i extract 'comment' fields, reflect not null in result.

i can extract fields (sq1comment; sq2comment, sq3comment) one-by-one query

db.collection.find({ "sq1comment": { $not: { $type: 10 } }) 

for output is:

[{ "_id": "1",   "sq1comment" : "in general aaaaaa.",    "sq3comment" : "a person bbbbb."  }] 

or if aggregation $project 'comment' fields nulls there:

db.collection.aggregate([{$project:     { "sq1comment": "$sq1comment",      "sq2comment": "$sq2comment",      "sq3comment": "$sq3comment"    } }]) 

for output is:

[{ "_id": "1",   "sq1comment" : "in general aaaaaa.",    "s2comment" : null,    "sq3comment" : "a person bbbbb."  },  { "_id": "2",    "sq1comment" : "in general cc.",     "s2comment" : "a story ff",     "sq3comment" : null  } ] 

i want project data show comment fields, entries not null. exclude null fields aggregation.


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -