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
62
votes
7 answers

Group result by 15 minutes time interval in MongoDb

I have a "status" collection like this strcture - { _id: ObjectId("545a0b63b03dbcd1238b4567"), status: 1004, comment: "Rem dolor ipsam placeat omnis non. Aspernatur nobis qui nisi similique.", created_at:…
Hein Zaw Htet
  • 941
  • 1
  • 9
  • 16
62
votes
2 answers

How can I sort a Meteor collection by time of insertion?

I am working on my first project using Meteor, and am having some difficulty with sorting. I have a form where users enter aphorisms that are then displayed in a list. Currently the most recent aphorisms automatically display at the bottom of the…
squeezemylime
  • 2,102
  • 3
  • 18
  • 26
61
votes
2 answers

How to insert an element to MongoDB internal list?

I have the following doc stored in MongoDB: { name: 'myDoc', list: [ { id:1 items:[ {id:1, name:'item1'}, {id:2, name:'item2'} ] }, { …
Guy Korland
  • 9,139
  • 14
  • 59
  • 106
58
votes
2 answers

Updating a Nested Array with MongoDB

I am trying to update a value in the nested array but can't get it to work. My object is like this { "_id": { "$oid": "1" }, "array1": [ { "_id": "12", "array2": [ { …
masanorinyo
  • 1,098
  • 2
  • 12
  • 25
58
votes
1 answer

MongoDB group by array inner-elements

I've got a list of articles, and each of them has an array property which lists various individuals mentioned in them: _id: { $oid: "52b632a9e4f2ba13c82ccd23" }, providerName: "The Guardian", url:…
Gil Adirim
  • 1,834
  • 2
  • 21
  • 33
57
votes
2 answers

MongoDB, performance of query by regular expression on indexed fields

I want to find an account by name (in a MongoDB collection of 50K accounts) In the usual way: we find with string db.accounts.find({ name: 'Jon Skeet' }) // indexes help improve performance! How about with regular expression? Is it an expensive…
damphat
  • 18,246
  • 8
  • 45
  • 59
56
votes
2 answers

In MongoDB how do you use $set to update a nested value/embedded document?

In MongoDB how do you use $set to update a nested value? For example, consider a collection people with the following document: { _id: ObjectId("5a7e395e20a31e44e0e7e284"), name: "foo", address: { street: "123", town: "bar" } } How do I…
Drew LeSueur
  • 19,185
  • 29
  • 89
  • 106
55
votes
5 answers

Moongoose aggregate $match does not match id's

I want to show products by ids (56e641d4864e5b780bb992c6 and 56e65504a323ee0812e511f2) and show price after subtracted by discount if available. I can count the final price using aggregate, but this return all document in a collection, how to make…
54
votes
7 answers

Query Mongodb on month, day, year... of a datetime

I'm using mongodb and I store datetime in my database in this way for a date "17-11-2011 18:00" I store: date = datetime.datetime(2011, 11, 17, 18, 0) db.mydatabase.mycollection.insert({"date" : date}) I would like to do a request like that month =…
kschaeffler
  • 4,083
  • 7
  • 33
  • 41
54
votes
3 answers

MongoDB nested array query

I've asked this as a comment on another question, and also posted a question on mongodb-user. No responses so far, so I'm resorting to asking a separate question. The documentation states: If the field holds an array, then the $in operator selects…
dgorur
  • 1,638
  • 4
  • 16
  • 26
53
votes
2 answers

Mongoose: how to use aggregate and find together

How can I use aggregate and find together in Mongoose? i.e I have the following schema: const schema = new Mongoose.Schema({ created: { type: Date, default: Date.now() }, name: { type: String, default: 'development' } followers: [{ type:…
Colin Wang
  • 6,778
  • 5
  • 26
  • 42
52
votes
7 answers

MongoDB 'unable to find index for $geoNear query'

I'm just trying to get a simple near query working. Here's a sample of my document. {"point": {"type": "Point", "coordinates": [30.443902444762696, -84.27326978424058]}, "created_on": {"$date": 1398016710168}, "radius": 180, …
frankV
  • 5,353
  • 8
  • 33
  • 46
51
votes
8 answers

How to get mongo command results in to a flat file

How do I export the results of a MongoDB command to a flat file For example, If I am to get db.collectionname.find() into a flat file. I tried db.collectionname.find() >> "test.txt" doesnt seem to work.
QVSJ
  • 1,165
  • 2
  • 12
  • 22
50
votes
5 answers

How to check if a pymongo cursor has query results

I need to check if a find statement returns a non-empty query. What I was doing was the following: query = collection.find({"string": field}) if not query: #do something Then I realized that my if statement was never executed because find returns a…
Alberto Coletta
  • 1,563
  • 2
  • 15
  • 24
50
votes
7 answers

Search on multiple collections in MongoDB

I know the theory of MongoDB and the fact that is doesn't support joins, and that I should use embeded documents or denormalize as much as possible, but here goes: I have multiple documents, such as: Users, which embed Suburbs, but also has: first…
Adrian Istrate
  • 631
  • 1
  • 5
  • 8