Questions tagged [mongodb-query]

This tag is for questions related to querying and updating MongoDB collections, either through the mongo shell or using a programming language driver.

This tag is for questions related to querying and updating MongoDB collections, either through the mongo shell or using a programming language driver.

Unlike most relational databases, MongoDB does not support SQL (Structured Query Language). Queries in MongoDB are expressed in the MongoDB Query Language which uses JSON from the mongo shell and BSON (Binary JSON) at the driver level.

MongoDB has a rich query language including many advanced operators as well as aggregation features such as the Aggregation Framework and Map-Reduce.

For effective query plans it is important to understand the indexing strategies and explain your slow queries to understand their index usage. By default, MongoDB will log all queries slower than 100ms (a slowms value that can be adjusted either as a command-line option or within the mongo shell).

MongoDB also includes a Database Profiler which can be enabled to capture either slow queries or all queries for a database.

Documentation

17312 questions
74
votes
3 answers

Is there an "upsert" option in the mongodb insert command?

I know this may be a silly question, but I read on an e-book that there is an upsert option in MongoDB insert. I couldn't find proper documentation about this. Can someone educate me about this?
astroanu
  • 3,901
  • 2
  • 36
  • 50
70
votes
4 answers

Mongoose find/update subdocument

I have the following schemas for the document Folder: var permissionSchema = new Schema({ role: { type: String }, create_folders: { type: Boolean }, create_contents: { type: Boolean } }); var folderSchema = new Schema({ name: {…
Darko Romanov
  • 2,776
  • 2
  • 31
  • 38
70
votes
4 answers

List all values of a certain field in mongodb

How would I get an array containing all values of a certain field for all of my documents in a collection? db.collection: { "_id" : ObjectId("51a7dc7b2cacf40b79990be6"), "x" : 1 } { "_id" : ObjectId("51a7dc7b2cacf40b79990be7"), "x" : 2 } { "_id" :…
chimpsarehungry
  • 1,775
  • 2
  • 17
  • 28
70
votes
4 answers

Is it possible to flatten MongoDB result query?

I have a deeply nested collection in my MongoDB collection. When I run the following query: db.countries.findOne({},{'data.country.neighbor.name':1,'_id':0}) I end up with this nested result here: {"data" : { "country" : [ { "neighbor"…
Marsellus Wallace
  • 17,991
  • 25
  • 90
  • 154
69
votes
3 answers

Conditional $sum in MongoDB

My collection in mongodb is similar to the following table in SQL: Sentiments(Company,Sentiment) Now, I need to execute a query like this: SELECT Company, SUM(CASE WHEN Sentiment >0 THEN Sentiment ELSE 0 END) AS SumPosSenti, SUM(CASE WHEN…
Aafreen Sheikh
  • 4,949
  • 6
  • 33
  • 43
68
votes
1 answer

Select MongoDB documents where a field either does not exist, is null, or is false?

Suppose I have a collection that contains the following documents: { "_id": 1, name: "Apple" } { "_id": 2, name: "Banana", "is_reported": null } { "_id": 3, name: "Cherry", "is_reported": false } { "_id": 4, name: "Kiwi", "is_reported": true } Is…
sffc
  • 6,186
  • 3
  • 44
  • 68
67
votes
4 answers

Mongo count occurrences of each value for a set of documents

I have some documents like this: { "user": '1' }, { "user": '1' }, { "user": '2' }, { "user": '3' } I'd like to be able to get a set of all the different users and their respective counts, sorted in decreasing order. So my output would be…
ritmatter
  • 3,448
  • 4
  • 24
  • 43
67
votes
4 answers

How do I rename fields when performing search/projection in MongoDB?

Is it possible to rename the name of fields returned in a find query? I would like to use something like $rename, however I wouldn't like to change the documents I'm accessing. I want just to retrieve them differently, something that works like…
themiurgo
  • 1,550
  • 2
  • 13
  • 16
67
votes
2 answers

Querying MongoDB to match in the first item in an array

I'm aware of the $in operator, which appears to search for an item's presence in array, but I only want to find a match if the item is in the first position in an array. For instance: { "_id" : ObjectId("0"), "imgs" : [ …
JVG
  • 20,198
  • 47
  • 132
  • 210
66
votes
6 answers

Mongoose document references with a one-to-many relationship

I'm working on designing a database structure for a new project, and I'm pretty new to MongoDB, and obviously Mongoose. I've read Mongooses population documentation, where it has a one-to-many relationship, with one Person document to many Story…
Justin
  • 1,959
  • 5
  • 22
  • 40
66
votes
3 answers

MongoDB aggregate within daily grouping

I have some docs in mongo that looks something like this: { _id : ObjectId("..."), "make" : "Nissan", .. }, { _id : ObjectId("..."), "make" : "Nissan", "saleDate" : ISODate("2013-04-10T12:39:50.676Z"), .. } Ideally, I'd like to be…
Kevin
  • 1,420
  • 2
  • 13
  • 11
65
votes
4 answers

Mongodb Join on _id field from String to ObjectId

I have two collections User { "_id" : ObjectId("584aac38686860d502929b8b"), "name" : "John" } Role { "_id" : ObjectId("584aaca6686860d502929b8d"), "role" : "Admin", "userId" : "584aac38686860d502929b8b" } I want to join these…
Kavya Mugali
  • 1,008
  • 2
  • 10
  • 17
65
votes
5 answers

Storing null vs not storing the key at all in MongoDB

It seems to me that when you are creating a Mongo document and have a field {key: value} which is sometimes not going to have a value, you have two options: Write {key: null} i.e. write null value in the field Don't store the key in that document…
Zaid Masud
  • 13,225
  • 9
  • 67
  • 88
64
votes
13 answers

how to convert string to numerical values in mongodb

I am trying to convert a string that contains a numerical value to its value in an aggregate query in MongoDB. Example of document { "_id": ObjectId("5522XXXXXXXXXXXX"), "Date": "2015-04-05", "PartnerID": "123456", "moop": "1234"…
Naftsen
  • 841
  • 1
  • 6
  • 7
62
votes
3 answers

Object type in mongoose

I am defining a mongoose schema and definition is as follows: inventoryDetails: { type: Object, required: true }, isActive:{ type:Boolean, default:false } I tried "Object" type and I am seeing my…
codewarrior
  • 813
  • 1
  • 6
  • 10