In Mongoose, how do I sort by date? (node.js)
let’s say I run this query in Mongoose: Room.find({}, (err,docs) => { }).sort({date:-1}); This doesn’t work! 11 Answers 11
let’s say I run this query in Mongoose: Room.find({}, (err,docs) => { }).sort({date:-1}); This doesn’t work! 11 Answers 11
I find no doc for the sort modifier. The only insight is in the unit tests: spec.lib.query.js#L12 writer.limit(5).sort([‘test’, 1]).group(‘name’) But it doesn’t work for me: Post.find().sort([‘updatedAt’, 1]); 20 Answers 20
Is there a function to turn a string into an objectId in node using mongoose? The schema specifies that something is an ObjectId, but when it is saved from a string, mongo tells me it is still just a string. The _id of the object, for instance, is displayed as objectId(“blah”). 8 Answers 8
I wanted to use the mongodb database, but I noticed that there are two different databases with either their own website and installation methods: mongodb and mongoose. So I came up asking myself this question: “Which one do I use?”. So in order to answer this question I ask the community if you could explain … Read more
Is there a way to add created_at and updated_at fields to a mongoose schema, without having to pass them in everytime new MyModel() is called? The created_at field would be a date and only added when a document is created. The updated_at field would be updated with new date whenever save() is called on a … Read more
I have a document from a mongoose find that I want to extend before JSON encoding and sending out as a response. If I try adding properties to the doc it is ignored. The properties don’t appear in Object.getOwnPropertyNames(doc) making a normal extend not possible. The strange thing is that JSON.parse(JSON.encode(doc)) works and returns an … Read more
I have a node.js application that pulls some data and sticks it into an object, like this: var results = new Object(); User.findOne(query, function(err, u) { results.userId = u._id; } When I do an if/then based on that stored ID, the comparison is never true: if (results.userId == AnotherMongoDocument._id) { console.log(‘This is never true’); } … Read more
I have a database wrapper class that establishes a connection to some MongoDB instance: async connect(connectionString: string): Promise<void> { this.client = await MongoClient.connect(connectionString) this.db = this.client.db() } This gave me a warning: (node:4833) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option … Read more
Basically I have a mongodb collection called ‘people’ whose schema is as follows: people: { name: String, friends: [{firstName: String, lastName: String}] } Now, I have a very basic express application that connects to the database and successfully creates ‘people’ with an empty friends array. In a secondary place in the application, a form is … Read more
If you have subdocument arrays, Mongoose automatically creates ids for each one. Example: { _id: “mainId” subDocArray: [ { _id: “unwantedId”, field: “value” }, { _id: “unwantedId”, field: “value” } ] } Is there a way to tell Mongoose to not create ids for objects within an array? 7 Answers 7