add created_at and updated_at fields to mongoose schemas

UPDATE: (5 years later) Note: If you decide to use Kappa Architecture (Event Sourcing + CQRS), then you do not need updated date at all. Since your data is an immutable, append-only event log, you only ever need event created date. Similar to the Lambda Architecture, described below. Then your application state is a projection … Read more

Avoid “current URL string parser is deprecated” warning by setting useNewUrlParser to true

Check your mongo version: mongo –version If you are using version >= 3.1.0, change your mongo connection file to -> MongoClient.connect(“mongodb://localhost:27017/YourDB”, { useNewUrlParser: true }) or your mongoose connection file to -> mongoose.connect(“mongodb://localhost:27017/YourDB”, { useNewUrlParser: true }); Ideally, it’s a version 4 feature, but v3.1.0 and above are supporting it too. Check out MongoDB GitHub … Read more

Comparing mongoose _id and strings

Mongoose uses the mongodb-native driver, which uses the custom ObjectID type. You can compare ObjectIDs with the .equals() method. With your example, results.userId.equals(AnotherMongoDocument._id). The ObjectID type also has a toString() method, if you wish to store a stringified version of the ObjectID in JSON format, or a cookie. If you use ObjectID = require(“mongodb”).ObjectID (requires … Read more

Push items into mongo array via mongoose

Assuming, var friend = { firstName: ‘Harry’, lastName: ‘Potter’ }; There are two options you have: Update the model in-memory, and save (plain javascript array.push): person.friends.push(friend); person.save(done); or PersonModel.update( { _id: person._id }, { $push: { friends: friend } }, done ); I always try and go for the first option when possible, because it’ll … Read more

mongodb/mongoose findMany – find all documents with IDs listed in array

The find function in mongoose is a full query to mongoDB. This means you can use the handy mongoDB $in clause, which works just like the SQL version of the same. model.find({ ‘_id’: { $in: [ mongoose.Types.ObjectId(‘4ed3ede8844f0f351100000c’), mongoose.Types.ObjectId(‘4ed3f117a844e0471100000d’), mongoose.Types.ObjectId(‘4ed3f18132f50c491100000e’) ]} }, function(err, docs){ console.log(docs); }); This method will work well even for arrays containing tens … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)