Skip to content

Tag: mongodb

How to new a NumberDecimal() in JavaScript Mongodb?

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…

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…

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 …