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

How to query for a dictionary in Mongo Document?

I have a Document, shown below This is my C# structure for that document public class MetaData { [BsonId] public ObjectId _Id { get; set; } [BsonElement("User")] public IDictionary User { get; set; } } public class…
3
votes
2 answers

MongoDB Driver Query * Filter Definition Builder * Nin $nin Not in filter

Has anyone used the C# .Net MongoDB Driver FilterDefinitionBuilder's not in filter? This is a simple example that I put together that I cannot seem to get to work. Assume that we must keep the collections as is. The goal is to retrieve only…
80sRocker
  • 267
  • 2
  • 5
  • 17
3
votes
1 answer

Save a subset of MongoDB(3.0) collection to another collection in Python

I found this answer - Answer link db.full_set.aggregate([ { $match: { date: "20120105" } }, { $out: "subset" } ]); I want do same thing but with first 15000 documents in collection, I couldn't find how to apply limit to such query (I tried using…
3
votes
0 answers

Can't connect MongoDB in Windows Server 2008 sometimes

My MongoDB service is running in Windows Server 2008 R2, but when I am trying to connect or access data it throws an error like "unable to connect" or "invalid access". These issues happen sometimes; if I restart the service again it works…
3
votes
2 answers

MongoDB aggregate - average on specific values in array of documents

I'm currently working on a database with the following structure: {"_id" : ObjectId("1abc2"), "startdatetime" : ISODate("2016-09-11T18:00:37Z"), "diveValues" : [ { "temp" : 15.269, "depth" : 0.0, }, { "temp" :…
Vegar
  • 33
  • 3
3
votes
2 answers

Selecting data from MongoDB where K of N criterias are met

I have documents with four fields: A, B, C, D Now I need to find documents where at least three fields matches. For example: Query: A=a, B=b, C=c, D=d Returned documents: a,b,c,d (four of four met) a,b,c (three of four met) a,b,d (another three of…
3
votes
0 answers

perform atomic aggregate read and update in mongo?

I have some documents with below format { "count_used" : Int64, "last_used" : Date, "enabled" : Boolean, "data" : String } I use the last_used field to sort on within an aggregate query so the last used document is served…
3
votes
1 answer

How to query BsonExtraElements in MongoDB via Linq

I used the mongodb [BsonExtraElements] feature to extend my class some dynamic data, but unfortunately I cannot create a query by mongodb C# driver. Here is my model class: public class MongoProductEntity { public MongoProductEntity() { …
Vajda Endre
  • 1,372
  • 15
  • 24
3
votes
2 answers

Convert SQL query to MongoDB aggregation where something="this" or something = "that"

Query: Select * from table1 where f1 = "val1" OR f1 = "val2" I need to convert this query to MongoDB query but using aggregation not find.
user1735921
  • 1,234
  • 17
  • 40
3
votes
3 answers

How to update property in multiple objects in an array for one document in Meteor collection/minimongo?

My question is almost a duplicate of this question. The difference is that I am using minimongo within the Meteor framework/platform. Given this document: { "_id" : ObjectId("4d2d8deff4e6c1d71fc29a07"), "user_id" :…
saejuro
  • 77
  • 7
3
votes
1 answer

get first day of month in mongo. convert 'yyyy-mm-dd-hh-mm-ss.SSS' into timestamp of 'yyyy-mm-01-00-00-00.000'

[ { "rating": 4, "createdAt": ISODate("2016-08-08T15:32:41.262+0000") }, { "rating": 4, "createdAt": ISODate("2016-08-08T15:32:41.262+0000") }, { "rating": 3, "createdAt": ISODate("2016-07-01T15:32:41.262+0000") …
nirvair
  • 3,099
  • 6
  • 38
  • 71
3
votes
1 answer

Get Number of weekdays between two dates in Mongo DB

I am new to Mongo DB. Can anyone help me in how to get the number of weekdays between two Given dates. $dayOfWeek of a date will give the value of the date 0 for sunday and 7 for saturday. But my question is how can i increment the date from start…
3
votes
3 answers

Retrieving ID of subdocument after update in MongoDB

I wrote a service to push a new data to my collection using update statement and I need to retrieve the id of last inserted data. I am using db.collection.update for this, but it just giving a response like this: { "result": { "ok": 1, …
midhun k
  • 1,004
  • 13
  • 33
3
votes
1 answer

Filtering an array inside an array in Mongo DB

I want to filter an array inside an array in Mongo DB. For example, given these 2 documents: { "_id" : 1.0, "pages" : [ { "texts" : [ { "text" : "foo" }, { "text" :…
Ivan
  • 263
  • 3
  • 11
3
votes
1 answer

Query subdocuments MongoDB Node.JS

I have a collection of objects which have a sub dictionary which contains data for a given institution, identified by the provider_id. How can I query a collection, products and return only produce which contain a given provider id? …
Clip
  • 2,737
  • 8
  • 36
  • 70