I tried to run some Javascript NumberDecimal tests in Mongodb3.4, but however the output is never correct, if I used the NumberInt instead, then the result is correct. Does Javascript unsupports NumberDecimal type? The following is my testing script: Thank you~ Answer The mongo shell (as at 3.4.5) does not su…
Tag: mongodb
Returning specific fields with mongoose
I’m trying to accomplish something really easy but still manage to fail. What I am trying to do is when I get a get request on my server I want to return all documents BUT just the specific fields populated. My schema goes as follows What I want is to return only client.phone and client.email plus order…
How to make Mongoose not insert empty array or object fields into a document
Let’s say we have a Mongoose schema in our Node.js project: And let’s we have an according object: Now to save this data into MongoDB we can use this code: Ok, while it’s all cool. But if data does not contain field_3 (array field, and the same will be for an object field) Mongoose will anyw…
What is the fastest way to update the whole document (all fields) in MongoDB?
Let’s say I want to update the whole document and override all fields, except _id. What of the three methods is the best in terms of resource consumption: 1. Set the complete document as update parameter, so all fields are passed Example: 2. Calculate a delta document between the original and the update…
mongoose save vs insert vs create
What are different ways to insert a document(record) into MongoDB using Mongoose? My current attempt: Any idea why insert and save doesn’t work in my case? I tried create, it inserted 2 document instead of 1. That’s strange. Answer The .save() is an instance method of the model, while the .create(…
Find closest entries in MongoDB to coordinates
I am having a hard time finding items which are closest to a set of lat/lon. My objects have their lat/lon stored as such: This is the query I am currently using, when I use it I get no results back, not even an empty array. collection.find({coordinates: {$near: [40.296898, -111.694647] }, $maxDistance:100}) …
How to use MongoDB with promises in Node.js?
I’ve been trying to discover how to use MongoDB with Node.js and in the docs it seems the suggested way is to use callbacks. Now, I know that it is just a matter of preference, but I really prefer using promises. The problem is that I didn’t find how to use them with MongoDB. Indeed, I’ve tr…
Multiple schema references in single schema array – mongoose
Can you populate an array in a mongoose schema with references to a few different schema options? To clarify the question a bit, say I have the following schemas: Can I populate the guns array with a bunch of ak47 OR m16? Can I put BOTH in the same guns array? Or does it require a populate ref in the
mongoose “Find” with multiple conditions
I am trying to get data from my mongoDB database by using mongoose filters. The scenario is that each user object in the database has certain fields like “Region” or “Sector”. Currently I am getting all the users that contain the keyword “region” in there object like so: Ho…
Find objects created in last week in mongo
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 …