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

Find by key for nested object in mongoose

I want to find a document based on key for nested object in mongoose. For example { "_id": 1, "first_name": "Tom", "email": "tom@example.com", "ACL": { 123gd: { read: true, write: false, }, …
Avinash A
  • 665
  • 1
  • 7
  • 22
3
votes
1 answer

How to find documents that their data match a condition on at least one item from a given array of items - mongoDB

I want to fetch records that exists in provided dates array. This provided array of dates will fetch records between start & end date. Example: I have this schema: [ { "_id": "1", "course": "Maths", "startDate":…
StormTrooper
  • 1,731
  • 4
  • 23
  • 37
3
votes
1 answer

MongoDB - $filter 2 array contains object

Let's say I have a document like bellow { fieldA : [ { _id : 1, value : 1, }, { _id : 2 value : 2, }, { _id : 3, value : 3, }, ], fieldB : [ { _id : 2 }, { _id…
vy.pham
  • 571
  • 6
  • 20
3
votes
0 answers

How to count occurances of special character in a string in mongodb using springboot?

In a mongo collection there are around 120k documents. For each document I have to check how many occurrences of special character '@' is in message field. Below is the document in json format [{ "_id":356, "message":"ty@78i@gh8" }] If it contains…
3
votes
2 answers

Join two collection in mongodb

I'm new in mongodb. Could you please tell me how to perform join operation in this. I've two collection: Collection 1 ("user") { _id: "d04d53dc-fb88-433e-a1c5-dd41a68d7655", userName: "XYZ User", age: 12 } Collection 2 ("square") { _id:…
3
votes
0 answers

Mongodb unmatch aggregate lookup

  I am very new in Mongodb and nodejs. I am trying to make attendance system and have multiple collection like attendances, shifts, leaves, Holidays. In the shifts collection have Week Off but obviously absents are not in record.   I have created a…
3
votes
2 answers

Insert data with new fields OR update conditionally using updateOne mongodb

I have a document with format like this: { "f1": "v1", "f2": { "id": 1, "sub": "subv", "updatedAt": 123 } } I have an another source that give me a inputf2 object. I want to write an upsert query to find document with matching…
Dinh Tien Loc
  • 131
  • 1
  • 6
3
votes
1 answer

Filter results in mongoDb based on status

I have an array like this [ { "name": "CAMP-1", "status": "incomplete", "version": 3, }, { "name": "CAMP-1", "status": "complete", "version": 2, }, { "name": "CAMP-1", "status": "complete", "version": 1, …
snehalg
  • 53
  • 5
3
votes
3 answers

Mongodb group by values and count the number of occurence

I am trying to count how many times does a particular value occur in a collection. { _id:1, field1: value, field2: A, } { _id:2, field1: value, field2: A, } { _id:3, field1: value, field2: C, } { _id:4, field1: value, …
user16463210
3
votes
3 answers

Mongodb query to get count of field based on the value for a matching string

I have the following Mongodb document. { "_id" : ObjectId("62406bfaa1d66f8d99c6e97d"), "skill": "Programming Language" "supply" : [ { "employeeName" : "A1", "skillRating" : 3 }, { "employeeName" : "A2", …
Mahavir Munot
  • 1,464
  • 2
  • 22
  • 47
3
votes
1 answer

mongo.isConnected alternative for node driver v4+

Is there any alternative for the deprecated MongoClient.isConnected in the native Node JS MongoDB driver? I read somewhere that one can handle the check via callbacks, which one can pass as parameters in the .connect Method (See the docs.) However,…
thelearner
  • 1,440
  • 3
  • 27
  • 58
3
votes
2 answers

How to select related events from the same table?

How do I make the next self query on MongoDB? SELECT e.user_id AS user_id, e.datetime AS started_at, (SELECT MIN(datetime) ## taking the closest "end" event datetime of that userId ## FROM events WHERE type = "end"…
Oded .S
  • 1,081
  • 2
  • 11
  • 18
3
votes
2 answers

$addToSet in update aggregation

I have a MongoDB update aggregation pipeline which I need to extend with $addToSet as I want to new elements to an array but I cannot mange to make it work with the aggregation pipeline. MongoDB Playground:…
quieri
  • 345
  • 3
  • 19
3
votes
1 answer

Mongodb for projects with many to many relationships

I'm at the beginning of starting a project for learning backend development with a bit of frontend development. For that, I wanted to create a project around cooking recipes. The plan was to create an admin REST API that would be later used by a…
Just A Question
  • 423
  • 6
  • 21
3
votes
1 answer

MongoDB generates same ObjectId with new ObjectId in pipeline's $project stage

As last stage of my pipeline, I have a $project like this : { ... anId : new ObjectId() } But mongo is generating the same Id for each document. I want it to generate a new different Id for each projected document. How to do so within the…
Marco
  • 322
  • 1
  • 3
  • 15
1 2 3
99
100