Questions tagged [mongodb-query]

This specific tag pertains to inquiries concerning the querying and updating of MongoDB collections. These actions can be performed either via the mongo shell or by utilizing a programming language driver.

Tips for enhancing query speed in mongodb

I have a huge MongoDB collection with over 5 million documents. Every time I try to add a new document, I need to check if there is already a document with the same title in the collection before inserting it. For example, let's say this is my MongoD ...

Pulling information from MongoDB within a specified price range using Node.js

I am currently working on fetching data within a specified price range from the property model. For example, if a user provides a minimum and maximum price, I want to display the number of properties that fall within that price range. I have indexed the ...

Is there a way to sort data by year and month in mongodb?

I'm trying to filter data by year in MongoDB based on a specific year and month. For example, if I pass in the year 2022, I only want to see data from that year. However, when I try using the $gte and $lte tags, it returns empty results. Can someone g ...

Why MongoDB's Push Operation Using Positional Operator isn't Functioning

In the 'users' collection, I have a document with the following data: { "_id" : "388179687996974", "matches" : [ { "userId" : "1495728740672094", "choice" : false, "dates" : [], "d ...

Searching for documents in MongoDB that meet specific criteria has become possible through the use

Criteria: COUNT the total number of documents in the collection WHERE objects.objectType is 'group' AND (objects.objectType is NOT 'person' AND relation is 'Exposed_to') Expectation: should return the count of all documents WHERE objects.objectType is 'gr ...

filtering the properties of mongoose documents

I have created a schema as shown below: var UserSchema = new Schema({ firstName: { type: String, required: true }, lastName: { type: String, required: true }, email: { type: String, required: true }, location: { type: String, required: true }, p ...

Exploring the power of dynamic routes in node.js to locate individuals within my mongoose database

app.get('/admin/reservas/:param', function(req, res) { var param = req.param("param"); console.log(param); mongoose.model('Something').findOne( { id: param }, function(err, obj) { ...

Incorporate data from two MongoDB collections using aggregation $lookup or populate in a Node.js application

Below are the schemas for two different collections. var activitySchema = new Schema({ activity_id: {type: String, index: {unique: true}, required: true}, begin_date : String, ... }) var registrationSchema = new Schema({ activit ...

Encountering difficulties while attempting to modify information in mongodb

Need help with updating specific fields in a Mongoose driver and Express JS setup. Here is the schema: var mongoose = require('mongoose'), Schema = mongoose.Schema; var ProfilesSchema = new Schema({ presentRound: { type: Numb ...

Updating multiple documents in mongoose using Node JS is a breeze

When attempting to update multiple documents using a query generated from an object array, I encountered an issue where only one document was actually modified. var split = num.toString().split(','); var list = new Array; for ( var i in split ) { var ...

Issues with MongoDB queries failing in live environments due to authentication problems

I'm currently developing a NodeJS application using Mongoose for database operations. Initially, I had no issues accessing my records in the "bears" database when authentication was disabled in Mongoose. However, upon enabling authentication and configuri ...

Trouble arises when attempting to showcase document fields in MongoDB

As a beginner in programming, I am putting in my best effort to figure things out on my own. However, I seem to be stuck without any guidance. I am attempting to display all products from the mongoDB based on their brand. While I have successfully set up a ...

Locate a subdocument by its unique identifier using the value in the main document

Suppose I have a document with the following structure: { selectedId: ObjectId("57b5fb2d7b41dde99009bc75"), children: [ {_id: ObjectId("57b5fb2d7b41dde99009bc75"), val: 10}, {_id: ObjectId("57b5fb2d7b41dde99009bc75"), val: 20}, ...

How to tailor the output of $group aggregation in node.js

When using aggregate in node.js, my code looks like this: collection.aggregate( { $group : { _id : "$id_page", "count" : {$sum : 1} } }, {$sort : {"count" : -1}}, {$limit : 1} ).limit(1).toArray(f ...

Utilizing mongoDB aggregation for unwinding, sorting, and grouping data

Let's say I have a users collection with the following documents: { _id: 1, hobbies: ['eat', 'read', 'swim'] }, { _id: 2, hobbies: ['eat', 'sl ...

Nested MongoDB object within multiple arrays

I need help with handling this JSON data structure: { data : { fields_data : [ [ { key1 : val }, { key1 : val } ], [ { key2 : val }, { key2 : val ...

A comprehensive guide on extracting matching date from MongoDB with Mongoose

I'm currently facing an issue with returning the appropriate records for a given date query in MongoDB and Mongoose. Despite my efforts, all rows are being returned instead. Below, you'll find the relevant code snippet as well as the model schema. Please ...

What is the correct way to utilize deleteMany() in the MongoDB shell when using an $and query?

I need to remove all entries in the infrastructure collection with a type.primary of "pipelines" and a type.secondary of "oil." Currently, I'm using this query: db.infrastructure.deleteMany({$and: [{"properties.type.primary": "pipelines&quo ...

Grouping nested arrays in a single document with MongoDB

My Collection is structured as follows: { id: 23423-dsfsdf-32423, name: Proj1, services: [ { id:sdfs-24423-sdf, name:P1_Service1, products:[{},{},{}] }, { id:sdfs-24jhh ...

Retrieve mongodb objects that fall within a specified date range

Within my collection, there is an example document structured as follows: { "_id" : ObjectId("5bbb299f06229dddbaab553b"), "phone" : "+38 (031) 231-23-21", "date_call" : "2018-10-08", "adress_delivery" : "1", "quantity_concrete" : "1", ...

Retrieve the number of occurrences of a particular field in the database

Seeking guidance on fetching and summing up the values of the "caseCount" objects within a schema. Can anyone provide me with assistance in accomplishing this task? Feel free to ask for further details if needed. Thank you! This is the Schema: const Profi ...

What is the code to retrieve all _id values within an array in MongoDB?

I currently possess the following assortment: { "_id" : ObjectId("5acdb95d5ea63a27c1facf92"), "venue" : ObjectId("5acdb95d5ea63a27c1facf8c"), "author" : ObjectId("5ac8ba3582c2345af70d4658"), } { "_id" : ObjectId("5acdb95d5ea63a27c1facf93") ...

Removing a document from an array within a MongoDB collection

I have a MongoDB schema that looks like this. { userID: 19202, products: [{ id: 020, name: 'first' }] } My goal is to remove items from the product array based on their id. I tried using the following command but it didn't give any errors and also ...

Search for records in MongoDB where the time is below a specified threshold

content of the document is provided below chatid:121212, messages:[ { msg:'Hello', time:'2021-04-17T16:35:25.879Z' }, . . . ] I am looking to retrieve all records where the timestamp is earlier than a ...

Ensuring uniformity in simultaneous read actions within MongoDB

I have a Node.js API called /create/appointment that performs two operations: STEP 1: Checking for an active appointment in the database. STEP 2: Creating an appointment if it does not exist. There are three collections: doctors patients appointments ...

Querying Mongodb with extended millisecond intervals

I have a collection that I need to export every 5 minutes based on the timestamp field. When querying the collection, the maximum date is as follows: db.testcol.find({},{_id : 0,ts : 1}).sort({ts:-1}) 2017-04-14 23:40:27.690Z I converted it to mil ...

Searching for fields in MongoDB using an AND clause to verify their existence

Within my collection, there are various documents that store information about individuals: { name : 'Peter' city : 'New York' dogs_owned : 2, cats_owned : 1, } { name : 'Amir' city : 'Chicago' } { name : 'James' city : 'Chi ...

Encountering the issue "error TS2339: Property 'user' is not available on type 'Object'."

issue TS2339: The property 'user' is not found on the type 'Object'. export class UserAuthentication implements OnInit { user = new BehaviorSubject<Userlogin>(null); constructor( private route: ActivatedRoute, private rou ...

Locate all records in Mongoose that do not have a specific value (using the $ne operator)

In my mongoose query, I am facing an issue where I need to select all rows except for specific ids. Here is a snippet of my code: var ids = [{id: 123},{id: 222},{id:333}]; User.find({_id: {$ne: ids.id }},'firstName lastName _id avatar',function (err,us ...

What is the process for making a specific query in MongoDB?

I currently have 3 blog posts stored in my MongoDB database and I am looking to retrieve a specific blog post based on its name. Here is an example of how the data is structured: [ { _id: ObjectId("61de0e1562abb7ffd4089373"), name: 'le ...

Combine data to calculate the average of both the document and the array elements

Here is an array containing objects: item1 = { name:'item', val:1, list:[ {type:'a',value:1}, {type:'b',value:1}, {type:'c',value:1} ] }; item2 = { name:'item', val:5, list:[ ...

Increase the value of a property for an element within an array object using MongoDB and Node.js

I am attempting to increase the value of a specific element in my array object "options": [ { "key": "banana", "votes": 0 }, { "key": "apple", "votes": 0 }, { "key": "mango", "votes": 0 ...

What is the best way to merge three collections without using $unwind and generate a nested outcome depending on a specific condition?

People Database: [ { "_id": ObjectId("5f3258cfbaaccedaa5dd2c96"), "gender": "male", "name": { "title": "mr", "first": "victor", " ...

Using node.js and mongoDB: Retrieve a custom value if the field is empty

My goal is to query the database for documents and if a certain field is not set, I want to replace it with a custom value. Let's say I have a collection of songs in my database where some have 'pathToCover' set and some don't. In those cases where it's no ...

Mongodb failing to recognize the concat function

I have a field within my collection that looks like this: uniqueId: 123 inTarefa: true exclude: "ab,cd," orderId: 987 I am attempting to update all of the values using a "FindOneAndUpdate" query like so: collection.findOneAndUpdate({ 'uniqu ...