Skip to content
Advertisement

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 support arithmetic with NumberDecimal values. The decimal type is not

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 orderdetails.status but still retain

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() is called directly from the Model as a method

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 can I query my objects to find

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 tried the following: And

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: How can put some conditions in mongoose that it return users that

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 now I am sorting

Advertisement