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.

Filter by
Sorted by
Tagged with
1732votes
45answers
1.4mviews

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.
user avatar
  • 184k
826votes
12answers
561kviews

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("...
user avatar
  • 8,815
515votes
16answers
883kviews

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" : { "...
user avatar
  • 32.4k
485votes
11answers
291kviews

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 ...
user avatar
445votes
15answers
398kviews

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" }, ...
user avatar
  • 6,592
403votes
16answers
339kviews

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 ...
user avatar
  • 235k
401votes
29answers
181kviews

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 ...
user avatar
  • 4,011
357votes
23answers
241kviews

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( { ...
user avatar
  • 4,699
287votes
11answers
398kviews

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.
user avatar
  • 3,097
281votes
7answers
346kviews

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?
user avatar
  • 19.1k
259votes
3answers
225kviews

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': "...
user avatar
  • 18.5k
223votes
16answers
155kviews

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: { "...
user avatar
  • 17.6k
180votes
8answers
130kviews

MongoDB vs Firebase [closed]

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 ...
user avatar
  • 8,079
177votes
9answers
221kviews

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 ...
user avatar
  • 5,278
150votes
6answers
85kviews

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 ...
user avatar
  • 1,501

15 30 50 per page
1
2 3 4 5
1018