How can I list all collections in the MongoDB shell?

You can do… JavaScript (shell): db.getCollectionNames() Node.js: db.listCollections() Non-JavaScript (shell only): show collections The reason I call that non-JavaScript is because: $ mongo prodmongo/app –eval “show collections” MongoDB shell version: 3.2.10 connecting to: prodmongo/app 2016-10-26T19:34:34.886-0400 E QUERY [thread1] SyntaxError: missing ; before statement @(shell eval):1:5 $ mongo prodmongo/app –eval “db.getCollectionNames()” MongoDB shell version: 3.2.10 connecting … Read more

MongoDB aggregate by field exists

I solved the same problem just last night, this way: > db.test.aggregate({$group:{_id:{$gt:[“$field”, null]}, count:{$sum:1}}}) { “_id” : true, “count” : 2 } { “_id” : false, “count” : 2 } See http://docs.mongodb.org/manual/reference/bson-types/#bson-types-comparison-order for a full explanation of how this works. Added From comment section: To check if the value doesn’t exist or is null use … Read more

Create filter aggregation in spring

You can try below query. Static Imports import static org.springframework.data.mongodb.core.aggregation.Aggregation.*; import static org.springframework.data.mongodb.core.aggregation.ArrayOperators.Filter.filter; import static org.springframework.data.mongodb.core.aggregation.ComparisonOperators.Eq.valueOf; Code Aggregation aggregation = newAggregation( project().and(filter(“parts”) .as(“item”) .by(valueOf( “item.currentState”) .equalToValue( “Estimation Confirmed”))) .as(“parts”); ); List<outputType> results = mongoTemplate.aggregate(aggregation, inputType, outputType)

MongoDB not equal to

Use $ne — $not should be followed by the standard operator: An examples for $ne, which stands for not equal: use test switched to db test db.test.insert({author : ‘me’, post: “”}) db.test.insert({author : ‘you’, post: “how to query”}) db.test.find({‘post’: {$ne : “”}}) { “_id” : ObjectId(“4f68b1a7768972d396fe2268”), “author” : “you”, “post” : “how to query” } … Read more