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

MongoDB get documents from a collection not in other collection

I'm trying to get the documents from a collection where aren't in other collection (the common NOT IN clause in SQL). If I run the next query: db.Companies_Movies.aggregate([ { $project: { "CompanyList.Movies.Code" :…
JulioC
  • 55
  • 6
3
votes
1 answer

Return only one field from MongoDB query

Suppose my document looks like this { "_id": 2, "Name": "Abcd", "Connected": [1,3] } Now, I want to retrieve the array [1,3] via the _id or Name. The purpose is to save this array into a JavaScript array. Hence I tried using…
mr.loop
  • 818
  • 6
  • 20
3
votes
1 answer

MongoDb operator not working for array of object properties

I am facing a problem with MongoDB query. Our collection named is products and the data is placed something like this. { "_id":"61b823681975ba537915cb0c", "salesInfo":{ "_id":"61b823681975ba537915c23c", "salesDate":[ …
Ven Nilson
  • 969
  • 4
  • 16
  • 42
3
votes
2 answers

MongoDB Aggregation - query collections that satisfies all array of objects not at least one

I have collections that have dates in an array like: datesArray: [{ start_date: Date, end_date: Date }] I want only those collections which satisfy all elements of datesArray. I am using it in aggregation $match operator…
3
votes
2 answers

How to perform lead and lag in MongoDB

I am using STudio 3T and I have query like this: select [Dashbo],lead([Dashbo]) over(order by [Entered Date]) from ATest_prevback; This is giving me and error. How to perform this in MongoDB? Can someone give me an example? Thanks, Adi
3
votes
1 answer

MongoDB running numbers within subsets of documents

I have a existing collection of ~12 million documents. I want to update one field in all the documents to have a running number within all groups of documents that share a common "ref" field. This would be a one time operation. Is there any way I…
Jolanen
  • 33
  • 3
3
votes
2 answers

filter object from array and select field

I've got a list of objects with name value pairs in them and I can't figure out how to retrieve a value from an array where name is a certain value. For instance: { "ordernumber" : "192915", "orderparameters" : { …
Serve Laurijssen
  • 9,266
  • 5
  • 45
  • 98
3
votes
0 answers

Golang MongoDB query returns empty cursor?

I'm at a bit of an impasse here with a query that SHOULD by all accounts, return results. If I run the following query in mongosh: db.events.aggregate([ {$unwind:"$features"}, {$match:{ $and:…
Jay
  • 75
  • 1
  • 6
3
votes
2 answers

MongoDB - How to convert string to date and perform a filter

How do I convert the field date of type String to Date before doing a filter to get all dates greater than the current date? Thanks!
SarahAlexa
  • 43
  • 1
  • 4
3
votes
1 answer

Fetch immediate next and previous documents based on conditions in MongoDB

Background I have the following collection: article { title: String, slug: String, published_at: Date, ... } MongoDB version: 4.4.10 The problem Given an article, I want to fetch the immediate next and previous articles depending on the…
Abhijit
  • 468
  • 8
  • 22
3
votes
0 answers

Can I take Backup of my database in mongodb atlas (free version)?

I am using a free version of mongoDB atlas database. I want to take a backup of my database. Whatever resources I found. they are suggesting that to take a backup I need to have a paid version. Is there any way by which I can take a backup. Manual…
3
votes
0 answers

Update multiple object values inside array in MongoDB with different values without looping, but pure query

I want to update this document in MongoDB [ { "persons" : [ { "name":"Javier", "occupation":"teacher" }, { "name":"Juliana", "occupation":"novelist" }, …
Zefan
  • 31
  • 2
3
votes
1 answer

Extract value of array and add in the same select mongoDB

I am new to the mongoDB aggregation and I have this situation. I have this Json and I need to convert by "select" this object: { "type": "PF", "code": 12345 "Name": Darth Vader, "currency": "BRL", "status": "SINGLE", …
3
votes
2 answers

Use Mongo aggregation to calculate potential cost

For the below collection, I want to calculate the potential cost of cooper_inc manufacturer in a particular shop. In our example's case, the shop name is 4AM Mart. There are three manufacturer suppliers to 4AM Mart = {Cooper_inc, jason_inc,…
CK5
  • 1,055
  • 3
  • 16
  • 29
3
votes
1 answer

Tried limiting the Group By data on mongo db 3.0

I have tried limiting the Group by data on Mongo DB 3.0 but seems like there is no proper option to do this. I want to display 5 latest records for each day based on this date dateCreated which is in array and corresponding records…
ranjan
  • 31
  • 2
1 2 3
99
100