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

MongoDB error while implementing $near query

Sample MongoDB Document { "_id":"5adedaad0bd4134kb0", "url":"https://iscon.cdnalign.com/t51.288515/s640x640e35/10249200241120_2108192950_n.jpg?ig_cache_key=MTEYUTB1NDgxN", "description":"#funinthesun", "locationName":"Calamari", …
Kunal Nischal
  • 64
  • 2
  • 6
3
votes
3 answers

how to use mongo $or with , when field doesn't exists

I need to find documents where one of the fields either has certain values, or doesn't exist. I have a query which works for checking the values, but I need to make it also check for the field not existing. My mongo query is like this currently: {…
user1324887
  • 526
  • 2
  • 5
  • 23
3
votes
2 answers

Mongo match in aggregate not working for date using java

I am using mongoDB java driver to query transactions between date range with aggregate framework. I am trying to use following mongo query: db.orders.aggregate([ { "$match":{ "order_commit_time": { "$gte":…
Himanshu Kansal
  • 410
  • 3
  • 15
3
votes
2 answers

Mongodb cannot use the part (...) to traverse the element ({...: undefined})]

After upgrade to 3.0 mongo driver i am receiving some new error on update request. For update like this: db.table.update({_id: .... } , {$set : { "tags.Tag1" : true }}); I am receiving cannot use the part (tags of tags.Tag1) to traverse the…
Oleg
  • 2,650
  • 3
  • 32
  • 45
3
votes
2 answers

Rename nested keys in mongodb

I have a collection "user" as follows: { _id: "123", "address": { "contact1":{ "cell":"98765412345" }, "contact2":{ "cell":"98765412346" } } } I want to rename the field 'cell' to 'mobile' in all the nested objects…
3
votes
1 answer

Mongo does not return documents in aggregation using $gte and $date

In the MongoDB (v3.2.8) I have some bucketed values stored in a time sequence. I'm currently trying to aggregate data out of these buckets using the Morphia framework (v1.1.0), limiting the documents by the date of the document (a field, see sample…
3
votes
2 answers

MongoDB - find all search string words in multiple fields

I'm trying to find all words in a search string across multiple fields. For example: If I were to search "java coffee" in this data: { _id: 1, name: "Java Hut", description: "Coffee and cakes" }, { _id: 2, name: "Burger Buns", description:…
Kendall
  • 33
  • 1
  • 4
3
votes
1 answer

Mongodb for each group find document with largest value of specific field (argmax)

After performing unwind in my aggreagate pipeline I have intermediate result such as : [ {_id:1, precision:0.91, recall:0.71, other fields...}, {_id:1, precision:0.71, recall:0.81, other fields...}, {_id:1, precision:0.61, recall:0.91, other…
Marcel
  • 915
  • 1
  • 10
  • 28
3
votes
2 answers

Unable to update the data in mongodb

I need to update some fields i am using mongoose driver and express js schema: var mongoose = require('mongoose'), Schema = mongoose.Schema; var ProfilesSchema = new Schema({ presentRound: { type: Number, default: 1 …
swathi anupuram
  • 725
  • 1
  • 7
  • 14
3
votes
2 answers

How to build Spark data frame with filtered records from MongoDB?

My application has been built utilizing MongoDB as a platform. One collection in DB has massive volume of data and have opted for apache spark to retrieve and generate analytical data through calculation. I have configured Spark Connector for…
Rubin Porwal
  • 3,218
  • 1
  • 17
  • 25
3
votes
1 answer

Find document with array that consists exclusively particular documents in MongoDB

I have collection path_test with 2 documents in it Document 1 { "_id" : 1, "tpc" : 5, "path" : [ { "nids" : [ 0, 10, 11 ], "ctc" : 2 }, { "nids" : [ 0, 10 ], "ctc" : 2 }, { "nids" : [ 0, 10,…
adpap
  • 179
  • 1
  • 7
3
votes
2 answers

mongo join between two collections

I have two mongo collections: defn "_id" : ObjectId("8570bebcb7db3"), "fields" : [ { "control" : { "appearance" : "field-list" }, "children" : [ { "bind" : { "required" : "yes" }, ... data "_id" :…
user2002858
  • 313
  • 2
  • 6
  • 17
3
votes
1 answer

Group and count the number of occurrences of an ObjectID stored in two separate fields

I am new to MongoDB and I am not sure how to solve in an efficient way this problem. In my app, a user can choose between one object or another (multiple times, by giving positive/negative ratings). What I want to do is to count how many times an…
3
votes
1 answer

Mongodb $sample after filtering

Let's say I want a person to find people they're not connected with, I would do: User.find({ _id: { $nin: req.user.connections }) However, I only want to retrieve at most 10 random documents from the return. In MongoDB, there is $sample: { $sample:…
db2791
  • 860
  • 3
  • 14
  • 28
3
votes
2 answers

Count Distinct Within Date Range

I have a MongoDB database with a collection of site-events. The documents look like: { "_id" : ObjectId("5785bb02eac0636f1dc07023"), "referrer" : "https://example.com", "_t" : ISODate("2016-07-12T18:10:17Z"), "_p" :…
Jared
  • 2,713
  • 10
  • 31
  • 59