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
1890
votes
46 answers

How to query MongoDB with "like"

I want to query something with SQL's like query: SELECT * FROM users WHERE name LIKE '%m%' How can I achieve the same in MongoDB? I can't find an operator for like in the documentation.
Freewind
  • 193,756
  • 157
  • 432
  • 708
944
votes
15 answers

Query for documents where array size is greater than 1

I have a MongoDB collection with documents in the following format: { "_id" : ObjectId("4e8ae86d08101908e1000001"), "name" : ["Name"], "zipcode" : ["2223"] } { "_id" : ObjectId("4e8ae86d08101908e1000002"), "name" : ["Another ", "Name"], …
emson
  • 10,005
  • 4
  • 24
  • 27
552
votes
17 answers

Find objects between two dates MongoDB

I've been playing around storing tweets inside mongodb, each object looks like this: { "_id" : ObjectId("4c02c58de500fe1be1000005"), "contributors" : null, "text" : "Hello world", "user" : { "following" : null, "followers_count" : 5, …
Tom
  • 33,626
  • 31
  • 85
  • 109
534
votes
12 answers

Update MongoDB field using value of another field

In MongoDB, is it possible to update the value of a field using the value from another field? The equivalent SQL would be something like: UPDATE Person SET Name = FirstName + ' ' + LastName And the MongoDB pseudo-code would be: db.person.update(…
Chris Fulstow
  • 41,170
  • 10
  • 86
  • 110
476
votes
19 answers

Retrieve only the queried element in an object array in MongoDB collection

Suppose you have the following documents in my collection: { "_id":ObjectId("562e7c594c12942f08fe4192"), "shapes":[ { "shape":"square", "color":"blue" }, { "shape":"circle", …
Sebtm
  • 7,002
  • 8
  • 29
  • 32
464
votes
18 answers

How to remove a field completely from a MongoDB document?

{ name: 'book', tags: { words: ['abc','123'], lat: 33, long: 22 } } Suppose this is a document. How do I remove "words" completely from all the documents in this collection? I want all documents to be without…
TIMEX
  • 259,804
  • 351
  • 777
  • 1,080
443
votes
30 answers

How can I get a random record from MongoDB?

I am looking to get a random record from a huge collection (100 million records). What is the fastest and most efficient way to do so? The data is already there and there are no field in which I can generate a random number and obtain a random row.
Will M
  • 4,431
  • 3
  • 16
  • 3
328
votes
8 answers

How to list all databases in the mongo shell?

I know how to list all collections in a particular database, but how do I list all available databases in MongoDB shell?
fracz
  • 20,536
  • 18
  • 103
  • 149
309
votes
11 answers

MongoDB: Combine data from multiple collections into one..how?

How can I (in MongoDB) combine data from multiple collections into one collection? Can I use map-reduce and if so then how? I would greatly appreciate some example as I am a novice.
user697697
  • 3,327
  • 3
  • 18
  • 12
294
votes
4 answers

How to query nested objects?

I have a problem when querying mongoDB with nested objects notation: db.messages.find( { headers : { From: "reservations@marriott.com" } } ).count() 0 db.messages.find( { 'headers.From': "reservations@marriott.com" } ).count() 5 I can't see what I…
Edmondo
  • 19,559
  • 13
  • 62
  • 115
247
votes
16 answers

How to Update Multiple Array Elements in mongodb

I have a Mongo document which holds an array of elements. I'd like to reset the .handled attribute of all objects in the array where .profile = XX. The document is in the following form: { "_id": ObjectId("4d2d8deff4e6c1d71fc29a07"), …
LiorH
  • 18,524
  • 17
  • 70
  • 98
194
votes
10 answers

mongodb: insert if not exists

Every day, I receive a stock of documents (an update). What I want to do is insert each item that does not already exist. I also want to keep track of the first time I inserted them, and the last time I saw them in an update. I don't want to have…
LeMiz
  • 5,554
  • 5
  • 28
  • 23
180
votes
8 answers

MongoDB vs Firebase

MongoDB vs Firebase What are some quantitative advantages of using Firebase over MongoDB? (not opinions) I know that Firebase is a cloud-based service with its own API, but I feel like Mongo may give me greater control in the long run.
itsclarke
  • 8,622
  • 6
  • 33
  • 50
177
votes
6 answers

Include all existing fields and add new fields to document

I would like to define a $project aggregation stage where I can instruct it to add a new field and include all existing fields, without having to list all the existing fields. My document looks like this, with many fields: { obj: { …
samuelluis
  • 1,771
  • 2
  • 11
  • 3
165
votes
10 answers

mongodb count num of distinct values per field/key

Is there a query for calculating how many distinct values a field contains in DB. f.e I have a field for country and there are 8 types of country values (spain, england, france, etc...) If someone adds more documents with a new country I would like…
Liatz
  • 4,997
  • 7
  • 28
  • 33
1
2 3
99 100