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
4
votes
3 answers

Reverse array field in MongoDB

I have a collection with a location field that was entered in the wrong order: location: [38.7633698, -121.2697997] When I try to place a 2d index on the field using ... db.collection.ensureIndex({'location': '2d'}); ... I get the following error…
matthoiland
  • 912
  • 11
  • 24
4
votes
1 answer

pymongo group by datetime

Im trying to search through a collection and group records by date field which is a datetime. I know pymongo converts those to the proper type on the background (ISODate or something like that). Question is, since datetime objects have date, time,…
Sebastian
  • 1,243
  • 1
  • 18
  • 34
4
votes
2 answers

Sort the values by Date in mongodb

I am new to mongodb and I am trying to sort all my rows by date. I have records from mixed sources and I trying to sort it separately. I didn't update the dateCreated while writing into db for some records. Later I found and I added dateCreated …
Vignesh Gopalakrishnan
  • 1,962
  • 9
  • 31
  • 53
4
votes
1 answer

MongoDB $pull array 2 level

I'm trying to pull an element in an array with e two level deep complexity My document : > db.users.find({ mail : 'titi@toto.fr'}).pretty() { "_class" : "bean.User", "_id" : ObjectId("52f504bb2f9dd91186211537"), "commandes" :…
4
votes
2 answers

How can I retrieve all the fields when using $elemMatch?

Consider the following posts collection: { _id: 1, title: "Title1", category: "Category1", comments: [ { title: "CommentTitle1", likes: 3 }, { …
rasco22862
  • 127
  • 1
  • 2
  • 6
4
votes
1 answer

How do you utilize $and, $or, $exists in the same Mongoose.js query?

I need to query documents for an account that either has no expiration[expires] (does not exist) or the expiration date is after 2/2/14. To do so, my mongodb query is: db.events.find({ _account: ObjectId("XXXX"), $or: [ {expires: {$gt:…
justin
  • 651
  • 1
  • 8
  • 18
4
votes
2 answers

How to count occurrences in nested document in mongodb?

In the situation where there is an array nested within an array, how would I count the number of a specific value? For example, I want to count the number of "answers" in the document below. query should return that there are 2 apples and 1…
user1099123
  • 6,063
  • 5
  • 30
  • 35
4
votes
1 answer

SyntaxError with pymongo and $ne : null

this is my code : #! /usr/bin/python import os from pymongo.connection import Connection from pymongo.master_slave_connection import MasterSlaveConnection database = 'toto' collection = 'logs' master = Connection(host="X.X.X.X",…
Adeel ASIF
  • 3,354
  • 9
  • 27
  • 44
4
votes
1 answer

How to get MongoCursorEnumerator with latest version C# (1.8.2+) driver?

Having some issues with code which used to work pre-1.8.2 version of C# MongoDB Driver: With previous releases of the driver, I could do something like: private MongoCursorEnumerator InitializeCursor() { var cursor =…
Val
  • 1,023
  • 3
  • 10
  • 21
4
votes
2 answers

$AddToSet to Dictionary (MongoDB, C#) duplicate entries

I have a MongoDB collection ("Users") which holds a dictionary field ("UserRegistrations"). The field definition is: BsonDictionaryOptions(DictionaryRepresentation.ArrayOfDocuments)] public Dictionary
mnnsx
  • 41
  • 5
4
votes
2 answers

How to search in every field of subdocument

Here is my document: { "_id" : ObjectId("5259b73b4949e0b22608960d"), "array" : [ "a", "b" ] } To find some value in the array field I use the following query: db.collection.find({roles: 'a'}) But I faced the problem, when the array field looks…
ozahorulia
  • 9,798
  • 8
  • 48
  • 72
4
votes
1 answer

MongoDB combining find with text search

I'm trying to filter a MongoDB collection with a .find() query and run a text search on the results to lower the cost of the query but I can't seem to be able to chain the commands. Here's what I've tried (that doesn't work): db.jobs.find({ …
A.M.K
  • 16,727
  • 4
  • 32
  • 64
4
votes
1 answer

Mongodb full text search matching precesion

I am trying to use mongodb full text search for showing suggestion will the user is typing. I have done all the necessary steps to create the text indexes and enable the full text search feature on the database and everything is working fine except…
maxsap
  • 2,971
  • 9
  • 44
  • 70
4
votes
3 answers

Array intersection in MongoDB

Ok there are a couple of things going on here..I have two collections: test and test1. The documents in both collections have an array field (tags and tags1, respectively) that contains some tags. I need to find the intersection of these tags and…
Aafreen Sheikh
  • 4,949
  • 6
  • 33
  • 43
4
votes
1 answer

How to retrieve all matching elements present inside array in Mongo DB?

I have document shown below: { name: "testing", place:"London", documents: [ { x:1, y:2, }, { …
Sachin
  • 3,424
  • 3
  • 21
  • 42