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

How to use MongoDB aggregation to find common items between one document and all others?

Now there is a likes collection, and the information recorded in it is as follows. // 1 { "_id": ObjectId("1"), "masterid": ObjectId("45678"), // user _id of the owner of the moment "momentid": ObjectId("123456"),// moment id …
lionjoy
  • 31
  • 1
3
votes
1 answer

I can sort on ISODate attributes, but the "$lt" and "$gt" queries are failing in my MongoDB Collections

I am centralizing data on my Atlas Cluster from bare-metal servers running community MongoDB databases locally. However I have an issue with my ISODates() attributes that are converted to "$date" dictionaries, after which the sort() query still…
Barthoche
  • 31
  • 5
3
votes
1 answer

Search field with objectID inside an array

I have an array of objectid stored inside a field called linkedAccounts like this: linkedAccount : [ {accountId:639f131640e86ede985400aa, comments:[]}, {accountId:639f131qwqwqe86ede985400aa, comments:[]}, {accountId:639f131640e86ede985400aa,…
Newbie_developer
  • 233
  • 1
  • 12
3
votes
2 answers

How to update a property of the last object of a list in mongo

I would like to update a property of the last objet stored in a list in mongo. For performance reasons, I can not pop the object from the list, then update the property, and then put the objet back. I can not either change the code design as it does…
lilgallon
  • 555
  • 1
  • 7
  • 14
3
votes
2 answers

Populate field then find - mongoose

I have a Cheques and a Payees collection, every cheque has its corresponding Payee ID. What I'm trying to do is to write some queries on cheques, but I need to preform the searching after populating the payee (to get the name) const search =…
3
votes
3 answers

MongoServerError: Cannot do exclusion on field date in inclusion projection

I was working with the MongoDB Atlas Server... and encountered this error... What does it mean...? Can someone explain in simple words plz... This was the query i was trying... db.posts.find({}, {title: 1, date: 0}) The structure of the posts in…
3
votes
1 answer

MongoDB fill missing dates in aggregation pipeline pagination

I have this pipeline : let pipeline = [ { $group: { _id: "$date", tasks: { $push: "$$ROOT" }, }, }, { $sort: { _id: -1 }, }, { $skip: skip //4,8,12,16...etc …
3
votes
2 answers

MongoDB remove objects from nested arrays based up on the condition using aggregation

I have a nested array, I have to remove the one object , based upon the condition using aggregation here is the JSON I get from mongodb { "_id": "633d275ceb34a28755974032", "name": "free", "products": [ { "title": "Product 1", …
3
votes
1 answer

Perform Calculation in MongoDb Query

Supposed I have the collections that contain Event type Product View water_heater View water_heater Purchase water_heater View television View television View television Purchase television There are two types of fields in…
3
votes
1 answer

Mongo performance is extremely slow for an aggregation query

Hope someone can help with the slow Mongo query - it runs fine against smaller collections but once we test it against the larger production collections, it fails with the message "Not enough disk space" even though we had limited the result set to…
3
votes
1 answer

Use dynamic object key as a `localField` and its value for `$lookup` aggregation stage

I have a schema: // mongoose schema const MySchema = new Schema({ objWithDynamicKeys: { type: Map, of: String } }); const OtherSchema = new Schema({ limit: Number, refToMySchema: { type: Schema.Types.ObjectId, ref: 'MyModel' }, name:…
3
votes
1 answer

Delete documents that satisfies multiple conditions MongoDB

I'm in the mongo shell and I have to remove documents that satisfies all of these conditions together: status: 'CO' renewPlan: true paymentType: 'credit_card' gatewayPayment: 'ebanx' Ive tried this but didnt work: db.transactions.deleteMany({…
Fernanda
  • 59
  • 6
3
votes
2 answers

Given an id of a document with recursive field `children`, find all documents that reference the document or any of its children

I have a collection of product folders productfolders and a collection of products products. const ProductFolderSchema = new Schema( { folderName: { type: String, required: true }, parent: { type: Schema.Types.ObjectId, ref:…
3
votes
1 answer

Why did the data get deleted when the condition did not match in the query

I have this schema in my mongo db. const movieSchema = new mongoose.Schema({ title: String, year: Number, score: Number, rating: String }) When I tried to delete collections with year greater than 1999, I have mistakenly put the…
3
votes
1 answer

Mongodb - Find coupled documents, where A is following B and B is following A

I'm trying to find all users in my database that follow the user back. The followers collection has 3 fields: _id, _t, _f. When a user follows another user it adds their user ID to _f and the id of the target user to _t I need to query all documents…
Dom
  • 31
  • 3