How to convert ISO Date to ‘yyyy-mm-dd hh:mm:ss’ date format in mongodb hashmap array?

There’s a ‘Created’ field in my collection, the date format is ISO date. How can I convert it to ‘yyyy-mm-dd hh:mm:ss’? Document sample: { “_id” : ObjectId(“432babb4d3281999g902a378”), “ID” : “290283667”, “Data” : { “Product-2713890” : { “Created” : ISODate(“2016-08-23T20:55:39.437Z”), “Product” : “Product-2713890” } } } Expected Result: { “_id” : ObjectId(“432babb4d3281999g902a378”), “ID” : “290283667”, “Product”…

how to configure mongodb index for sorting by value within time range?

I have a collection like below tx_collection – value – from – to – timestamp What I want to extract from the collection is the highest 100 values within certain time period. For example, db.getCollection(‘tx_collection’).find({“timestamp”:{$gte:ISODate(“2018-01-10T00:00:00.000Z”)}}).sort({“value”:-1}).limit(100) For query performance, I created index like {value: -1, timestmap: 1} But, when I run find query with the index,…

How to update subdocument array item in mongodb

I have a document like this: { “name”:”customer1″, “documents”:[ { “path”:”path1″, “description”:”desc1″ }, { “path”:”path2″, “description”:”desc2″ } ] } I’m trying to update all fields of a document but like this: const updatedCustomer=new documentModel(req.body); customer.documents.id(req.params.documentId)=updatedCustomer; But if I don’t state a field name, document is not updating. customer.documents.id(req.params.documentId).path=updatedCustomer.path; How can I do that?

Return MongoDB query result as variable instead of to console

In a javascript file named db_to_var.js I have the following function that succesfully prints the array returned by a query to database test_db, to console.log() . (E.g. a database called cars which have a key name describes the brandname, e.g. toyota, hyunday,volvo.). The method is called with: console.log(“listCars function call=”+listCars()); and contains: function listCars() {…

MongoDB: find elements where element id is in array of ids

I am working with a collection of users like so { “_id” : ObjectId(“5dc822c6547b8500340b2c34”), “firstName” : “SomeFirstName”, “lastName” : “SomeLastName”, “uniqueId” : “HF2134fGA” } { “_id” : ObjectId(“b80340b2c34522c65450dc87”), “firstName” : “AnotherFirstName”, “lastName” : “AnotherLastName”, “uniqueId” : “4F21fGH3A” } Also a list of Object like so [{name: “Name”, id:”4F21fGH3A”}, {name: “Name”, id:”21fG34HFA”}] I would like to…