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

13762 questions
1607
votes
44 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
  • 177,284
  • 143
  • 381
  • 649
746
votes
14 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
  • 8,015
  • 4
  • 19
  • 27
472
votes
14 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
  • 30,868
  • 31
  • 81
  • 104
430
votes
10 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
  • 38,141
  • 9
  • 83
  • 109
416
votes
14 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
  • 6,222
  • 8
  • 27
  • 32
369
votes
15 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
  • 217,272
  • 324
  • 727
  • 1,038
368
votes
28 answers

Random record from MongoDB

I am looking to get a random record from a huge (100 million record) mongodb. 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. Any…
Will M
  • 3,681
  • 3
  • 14
  • 3
339
votes
21 answers

Get names of all keys in the collection

I'd like to get the names of all the keys in a MongoDB collection. For example, from this: db.things.insert( { type : ['dog', 'cat'] } ); db.things.insert( { egg : ['cat'] } ); db.things.insert( { type : [] } ); db.things.insert( { hello : [] }…
Steve
  • 4,509
  • 5
  • 19
  • 16
255
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
  • 2,777
  • 3
  • 16
  • 12
250
votes
7 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
  • 18,175
  • 16
  • 93
  • 143
233
votes
3 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…
Edmondo1984
  • 17,841
  • 12
  • 55
  • 99
202
votes
15 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
  • 16,760
  • 15
  • 67
  • 91
163
votes
8 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,068
  • 5
  • 25
  • 23
135
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,351
  • 2
  • 9
  • 3
120
votes
7 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,055
  • 6
  • 24
  • 33
1
2 3
99 100