MongoDB nested array query

After running some queries, I came to the conclusion that $in doesn’t work for an array of arrays. You can use $elemMatch instead and it’ll work, but it is frustrating that MongoDB’s documentation doesn’t warn about it. I created this document: { “_id”: “51cb12857124a215940cf2d4”, “level1”: [ [ “item00”, “item01” ], [ “item10”, “item11” ] ], … Read more

MongoDB cannot start server: The default storage engine ‘wiredTiger’ is not available with this build of mongod

Well… There appears to be a version conflict: you are probably running a 32bit version of Mongo. Just do as they say and actually use the other default storage engine: Write the command as follows in your Mongo/bin directory: mongod –storageEngine=mmapv1 –dbpath [your-path] Should solve the problem. I guess you don’t quite mind about using … Read more

Mongoose limit/offset and count query

I suggest you to use 2 queries: db.collection.count() will return total number of items. This value is stored somewhere in Mongo and it is not calculated. db.collection.find().skip(20).limit(10) here I assume you could use a sort by some field, so do not forget to add an index on this field. This query will be fast too. … Read more

How to stop insertion of Duplicate documents in a mongodb collection

Don’t use insert. Use update with upsert=true. Update will look for the document that matches your query, then it will modify the fields you want and then, you can tell it upsert:True if you want to insert if no document matches your query. db.collection.update( <query>, <update>, { upsert: <boolean>, multi: <boolean>, writeConcern: <document> } ) … Read more