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
1 answer

MongoDB two seemingly identical queries - one uses an index the other doesn't

I noticed in Mongo logs that some queries were taking longer than expected. Fri Jan 4 08:53:39 [conn587] query mydb.User query: { query: { someField: "eu", lastRecord.importantValue: { $ne: nan.0 }, lastRecord.otherValue: { $gte: 1000 } },…
John M
  • 1,469
  • 17
  • 41
4
votes
2 answers

Getting only the first item for an array property in mongodb

Given the following layout in a collection... { vehicle_id: 1 ,// bunch of properties I don't want ,vehicle: { mfg_year: 1928 ,mfg_make: "Ford" ,mfg_model: "Model A" ,mfg_trim: "T-Bucket" ,// bunch of properties I don't…
Tracker1
  • 19,103
  • 12
  • 80
  • 106
4
votes
3 answers

MongoDB - Contains (LIKE) query on concatenated field

I am new in MongoDB. I am programming an application with spring data and mongodb and I have one class with two fields: firstname and lastname. I need one query for documents that contain one string in the full name (firstname + lastname). For…
4
votes
3 answers

mongodb java to insert embedded document

I have a collection with embedded documents in it. System { System_Info: ..., Tenant: [ { Tenant_Id: ..., Tenant_Info: ..., Prop_Info: ... }, { Tenant_Id: ..., Tenant_Info: ...,…
Ramya
  • 1,067
  • 6
  • 16
  • 29
4
votes
1 answer

Upsert Multiple Documents while using $in in the update selector

I'm writing a procedure that will take an array of strings such as var mywords = ["cat", "dog", "fish"] and upsert them into a MongoDb collection (called the 'words' collection). I would like to keep a count of how many times each word has been…
dgh
  • 8,969
  • 9
  • 38
  • 49
4
votes
2 answers

mongodb aggregation php

I'm using mongodb 2.1 and how to translate this query into php db.counter.aggregate([ { $match:{ page_id:123456 }}, { $group:{_id:"$page_id",total:{$sum:"$pageview"}} } ]); Thanks
user1457750
  • 53
  • 1
  • 2
  • 4
4
votes
1 answer

How can I store and query recurring date events using mongo?

I'd like to allow my users to setup a schedule for their events. It could be a single day, or for convenience I'd like to allow them to specify a reoccurring event (similar to an Outlook appointment). Storing a single day would be pretty easy, but…
rball
  • 6,925
  • 7
  • 49
  • 77
4
votes
2 answers

MongoDB unique value aggregation via map reduce

I see plenty of questions on SO about aggregation in MongoDB, however, I have not found a complete solution to mine yet. Here's an example of my data: { "fruits" : { "apple" : "red", "orange" : "orange", "plum" :…
SteveK
  • 996
  • 1
  • 8
  • 11
4
votes
1 answer

How to query a sub document collection using MongoDB and C# driver

I have the following structure: public class ThreadDocument { public ThreadDocument() { Messages = new List(); Recipients = new List(); } [JsonIgnore] public ObjectId Id { get; set; } …
rball
  • 6,925
  • 7
  • 49
  • 77
4
votes
2 answers

Get nested fields with MongoDB shell

I've "users" collection with a "watchlists" field, which have many inner fields too, one of that is "arrangeable_values" (the second field within "watchlists"). I need to find for each user in "users" collection, each "arrangeable_values" within…
Luca G. Soave
  • 12,271
  • 12
  • 58
  • 109
3
votes
1 answer

What is the fundmental difference between MongoDB / NoSQL which allows faster aggregation (MapReduce) compared to MySQL

Greeting! I have the following problem. I have a table with huge number of rows which I need to search and then group search results by many parameters. Let's say the table is id, big_text, price, country, field1, field2, ..., fieldX And we run a…
Eugene
  • 4,197
  • 7
  • 37
  • 54
3
votes
0 answers

Can mongo integrate JSON from a document prop into a query?

We have some documents in mongo that are essentially saved filters consisting of a name string and query JSON, along with some other properties, so for example: { name: "myFilter", query: { prop1: "val1", prop2: "val2" }, groupId:…
usernamenumber
  • 149
  • 1
  • 8
3
votes
1 answer

How to lookup in the same document under a collection in MongoDB

I am having below document in MongoDB [UPDATE : Shortened the document] { "sections": [ { "_id": { "$oid": "64cbeb62b669cd29a5719a8f" }, "categories": [ { "measureValues": [ …
3
votes
1 answer

Mongodb calculate product stock from sales and purchase

I was trying to build simple billing application. I have store product opening quantity, and sales and purchases along with product id. I need product wise current stock of all products on Products collection. I have created a Playground. Products…
Pallab
  • 145
  • 1
  • 9
3
votes
0 answers

MongoDB timeseries: find documents by the "metaField"

I have defined the following TS collection: db.createCollection( "values", { timeseries: { timeField: "timestamp", metaField: "meta", granularity: "minutes" } } ) my meta value is a comprised…
Elimination
  • 2,619
  • 4
  • 22
  • 38