How to get all the values that contains part of a string using mongoose find?

You almost answered this yourself in your tags. MongoDB has a $regex operator which allows a regular expression to be submitted as a query. So you query for strings containing “Alex” you do this: Books.find( { “authors”: { “$regex”: “Alex”, “$options”: “i” } }, function(err,docs) { } ); You can also do this: Books.find( { … Read more

MongoDB – The argument to $size must be an Array, but was of type: EOO / missing

You can use the $ifNull operator here. It seems the field is either not an array or not present by the given error: { “$project”: { “people”: 1, “Count”: { “$size”: { “$ifNull”: [ “$myFieldArray”, [] ] } } }} Also you might want to check for the $type in your $match in case these … Read more

Get a count of total documents with MongoDB when using limit

Mongodb 3.4 has introduced $facet aggregation which processes multiple aggregation pipelines within a single stage on the same set of input documents. Using $facet and $group you can find documents with $limit and can get total count. You can use below aggregation in mongodb 3.4 db.collection.aggregate([ { “$facet”: { “totalData”: [ { “$match”: { }}, … Read more

How to use MongoDBs aggregate `$lookup` as `findOne()`

You’re almost there, you need to add another $project stage to your pipeline and use the $arrayElemAt to return the single element in the array. db.users.aggregate( [ { “$project”: { “fullName”: { “$concat”: [ “$firstName”, ” “, “$lastName”] }, “country”: “$country” }}, { “$lookup”: { “from”: “countries”, “localField”: “country”, “foreignField”: “_id”, “as”: “countryInfo” }}, { … 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

MongoDB Full and Partial Text Search

As at MongoDB 3.4, the text search feature is designed to support case-insensitive searches on text content with language-specific rules for stopwords and stemming. Stemming rules for supported languages are based on standard algorithms which generally handle common verbs and nouns but are unaware of proper nouns. There is no explicit support for partial or … Read more