Does Mongoose support the Mongodb `findAndModify` method?

The feature is not well (read: at all) documented, but after reading through the source code, I came up with the following solution. Create your collection schema. var Counters = new Schema({ _id: String, next: Number }); Create a static method on the schema which will expose the findAndModify method of the model’s collection. Counters.statics.findAndModify … Read more

PyMongo vs MongoEngine for Django

This is an old question but stumbling across it, I don’t think the accepted answer answers the question. The question wasn’t “What is MongoEngine?” – it was “Why should I use MongoEngine?” And the advantages of such an approach. This goes beyond Django to Python/Mongo in general. My two cents: While both PyMongo and MongoEngine … Read more

Mongoose, CastError: Cast to Array failed for value when trying to save a model that contains a model

Man, I had a similar issue creating an Schema like this: QuestionnaireSchema = mongoose.Schema({ formId: Number, name: String, questions: [ { type: String, title: String, alternatives:[{ label: String, value: “Mixed” }] } ] }); My mistake was that I am using “type” as a field name and this is reserved word in mongoose. I just … Read more

understand MongoDB cache system

Note: This was written back in 2013 when MongoDB was still quite young, it didn’t have the features it does today, while this answer still holds true for mmap, it does not for the other storage technologies MongoDB now implements, such as WiredTiger, or Percona. A good place to start to understand exactly what is … Read more

Sorting on Multiple fields mongo DB

The MongoDB query optimizer works by trying different plans to determine which approach works best for a given query. The winning plan for that query pattern is then cached for the next ~1,000 queries or until you do an explain(). To understand which query plans were considered, you should use explain(1), eg: db.col.find({category:’A’}).sort({updated: -1}).explain(1) The … Read more