I have a collection which has a field called timestamp containing date object. I have this query: Also if it is possible, Can I sort these returned documents by length of an array in this document. Here is the schema: I want to sort the returned objects by size of difference of votes.up and votes.down. Right now I am sorting
Tag: mongoose
Mongoose – What does the exec function do?
I came across a piece of Mongoose code that included a query findOne and then an exec() function. Ive never seen that method in Javascript before? What does it do exactly? Answer Basically when using mongoose, documents can be retrieved using helpers. Every model method that accepts query conditions can be executed by means of a callback or the exec
Mongoose date field – Set default to date.now + N days
In a mongoose schema such as: On the line for the “end” field the default date should set to +7 days. I can add presave hook and set it there, but wondering if theres a way to do this inline in the default field. Answer You can add 7 days converted to milliseconds to current date like this or even
Mongoose JS findOne always returns null
I’ve been fighting with trying to get Mongoose to return data from my local MongoDB instance; I can run the same command in the MongoDB shell and I get results back. I have found a post on stackoverflow that talks about the exact problem I’m having here; I’ve followed the answers on this post but I still can’t seem to
Shorten ObjectId in node.js and mongoose
my URLs look like this at the moment: So, as you can see, it gets pretty long, pretty fast. I was thinking about shortening these ObjectIds. Idea is that I should add new field called “shortId” to every model in my database. So instead of having: we would have this: I found a way to do it like this: But
Mongoose auto increment
According to this mongodb article it is possible to auto increment a field and I would like the use the counters collection way. The problem with that example is that I don’t have thousands of people typing the data in the database using the mongo console. Instead I am trying to use mongoose. So my schema looks something like this:
Return certain fields with .populate() from Mongoose
I’m getting returned a JSON value from MongoDB after I run my query. The problem is I do not want to return all the JSON associated with my return, I tried searching the docs and didn’t find a proper way to do this. I was wondering what if it is at possible, and if so what is the proper way
Find documents with array that doesn’t contains a specific value
I have the following model: I am looking for a query that retrieves all the Persons that are not part of a certain Group (i.e the persons’ group array doesn’t contain the id of the specified group). I was thinking about something like this, but I’m not sure it is correct: Person.find({groups: {$nin: [group._id]}) Answer Nothing wrong with what you
Mongoose find/update subdocument
I have the following schemas for the document Folder: So, for each Page I can have many permissions. In my CMS there’s a panel where I list all the folders and their permissions. The admin can edit a single permission and save it. I could easily save the whole Folder document with its permissions array, where only one permission was
Multiple populates – mongoosejs
Just a simple query, for example with a double ref in the model. Schema / Model Query I already tried something like In fact only one of the populates works. So, how do is get two populates working ? Answer You’re already using the correct syntax of: Perhaps the meal ObjectId from the order isn’t in the Meals collection?