mongoose: find value inside object, inside array inside array of objects - mongodb

Hello and sorry for the title,
it is quite hard to explain with words.
I have a data with multiples level :
-strategyParameters -> [wsent -> [ {confirmationCandle.timestamp} ] ]
I am trying to request documents, having inside wSent, inside strategyParameters, a confirmationCandle timestamp greater than a certain value.
I have tried several attemps with some things similar to this :
db.getCollection('users').find({"exchange":"binance","pair":"LTCUSDT","timeframe":"1h","wSent.confirmationCandle.timestamp":{"$gt":1606644800000}})
But it was unsuccessful, any help would be much appreciated.
bellow a concrete example of document inside my db:
{
"_id" : ObjectId("5fd7b0f1356b89312949963a"),
"email" : "test#test.com",
"password" : "$2b$10$egeg",
"strategyParameters" : [
{
"_id" : ObjectId("5fd7c9940d0f3033fc527547"),
"pair" : "LTCUSDT",
"strategy" : "w",
"timeframe" : "1h",
"exchange" : "binance",
"wSent" : [
{
"_id" : ObjectId("5fd7adb9d157b430a05a87b1"),
"firstBottom" : {
"open" : 79.46,
"high" : 80.07,
"low" : 78.29,
"close" : 78.91,
"timestamp" : 1606690800000.0,
"isTop" : false,
"isBottom" : true
},
"top" : {
"open" : 78.89,
"high" : 80.5,
"low" : 78.87,
"close" : 79.7,
"timestamp" : 1606694400000.0,
"isTop" : true,
"isBottom" : false
},
"seconBottom" : {
"open" : 79.73,
"high" : 79.84,
"low" : 78.55,
"close" : 79.29,
"timestamp" : 1606698000000.0,
"isTop" : false,
"isBottom" : true
},
"confirmationCandle" : {
"open" : 81.56,
"high" : 85,
"low" : 81.1,
"close" : 83.24,
"timestamp" : 1606744800000.0, <-- the target
"isTop" : false,
"isBottom" : false
},
"exchange" : "binance",
"pair" : "LTCUSDT",
"timeframe" : "1h"
}
]
}
]
}

Your query doesn't match because the fields you're searching for don't align with the document's structure. The sample document doesn't have exchange, pair, timeframe or wSent.confirmationCandle.timestamp fields. But it does have strategyParameters.wSent.exchange, strategyParameters.wSent.pair, strategyParameters.wSent.timeframe and strategyParameters.wSent.confirmationCandle.timestamp fields.
Your query should look something like this:
db.getCollection("users").find({
"strategyParameters.wSent.exchange": "binance",
"strategyParameters.wSent.pair": "LTCUSDT",
"strategyParameters.wSent.timeframe": "1h",
"strategyParameters.wSent.confirmationCandle.timestamp": { $gt :1606644800000 }
})

Related

How to find documents using a field of an embedded document that is inside an array?

I have the Data/Schema for my student records:
{
"_id" : ObjectId("579ed0ba7d509178a97fae8f"),
"fullName" : "ABC",
"enroll" : "AB1234",
"profile" : {
"isCompleted" : true,
"verification" : [
{
"pro" : true,
"verifiedBy" : "ProAct",
"verifiedOn" : ISODate("2016-09-12T07:36:53.680Z")
},
],
"isChecked" : false,
"fullName" : "ABC",
"gender" : "Male",
"emergencyContactPerson" : {
"name" : "Father",
"mobile" : "9412345678",
"email" : "example#gmail.com",
},
},
"contact" : {
"emailID" : {
"isVerified" : true,
"isChecked" : false,
"verificationTokenExpiresIn" : ISODate("2016-08-01T05:35:00.218Z"),
"verifiedOn" : ISODate("2016-08-01T04:35:10.992Z")
},
"mobileID" : {
"isVerified" : true,
"isChecked" : false,
"verificationTokenExpiresIn" : ISODate("2016-08-01T04:42:45.206Z"),
"verifiedOn" : ISODate("2016-08-01T04:33:36.692Z")
}
},
"services" : [
{
"applied" : true,
"appliedOn" : ISODate("2016-09-17T03:01:49.829Z"),
"status" : "Created",
"mac" : "70-77-88-00-AA-BB",
"createdBy" : "Example"
},
]
}
How can I build query, to get student info with the help of mac address 70-77-88-00-AA-BB. i only know mac address value?
i am new here please help me out
Query
services.mac is an array of all the mac addresses members of services
in querying(not in aggregation $eq) an array is equal with a value if it contains that value, so the above will work
the bellow query returns all the students, that have that mac adrress on the services array
if you want to make it fast you can also create an index on "services.mac"
*read mongodb documentation its very good and with examples
Test code here
db.collection.find({
"services.mac": "70-77-88-00-AA-BB"
})

Updating two level sub-document which is list in mongoDB?

I have a hotel collection whose one of the document looks like this -
{
"_id" : "HOTEL_1",
"name" : "Decent hotel",
"chainId" : "CHN123",
"rooms" : [
{
"id" : "ROM1",
"name" : "decent rooms",
"ratePlans" : [
{
"ratePlanId" : "RPNB1191989873C2G",
"status" : "INACTIVE",
"marginPart" : {
"marginType" : "PERCENTAGE",
"margin" : "32"
}
},
{
"ratePlanId" : "RPNE0992HBG6I0GE8",
"status" : "INACTIVE",
"marginPart" : {
"marginType" : "PERCENTAGE",
"margin" : "32"
}
}
]
},
{
"id" : "ROM2",
"name" : "another decent rooms"
"ratePlans" : []
}
]
}
I need to update status as ACTIVE of all the rate plans of all the rooms with a certain condition like chainId.
I tried with this but failed -
db.hotel.updateMany({ "chainId" : "CHN_123"},{$set : {"rooms.$ratePlans.$status" : "ACTIVE" }});
I also want to update margin as common value say 50% to all such rates.
Instead of updateMany try the below query
db.hotel.update({ "chainId" : "CHN_123"},{$set : {"rooms.$ratePlans.$status" : "ACTIVE" }},{multi:true,upsert:false},function(err,doc){
console.log(doc)
});
It works always!!

$near query returns 0 results after adding $maxDistance

I have a database with structure like this :
{
"_id" : ObjectId("5785663bda7b71a0c5601c89"),
"visible" : true,
"created" : {
"changeset" : "29670315",
"user" : "samit_53",
"version" : "1",
"uid" : "2297171",
"timestamp" : "2015-03-23T03:59:14Z"
},
"type" : "node",
"id" : "3413812873",
"pos" : [
22.1173487,
88.0034742
]
}
Now I have '2d' index on pos
>>db.kol_sample.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"ns" : "project_sample.kol_sample",
"name" : "_id_"
},
{
"v" : 1,
"key" : {
"pos" : "2d"
},
"ns" : "project_sample.kol_sample",
"name" : "pos_2d"
}
]
Now when I query with only the $near I get 100 results
db.kol_sample.findOne({"pos":{$near:[22,88]}})
{
"_id" : ObjectId("5785663bda7b71a0c5601c89"),
"visible" : true,
"created" : {
"changeset" : "29670315",
"user" : "samit_53",
"version" : "1",
"uid" : "2297171",
"timestamp" : "2015-03-23T03:59:14Z"
},
"type" : "node",
"id" : "3413812873",
"pos" : [
22.1173487,
88.0034742
]
}
But after I add the $maxDistance param no matter what distance I pass it always returns null
db.kol_sample.findOne({"pos":{$near:[22.1173487,88.0034742]},$maxDistance:100})
null
MongoDB shell version: 2.4.9 32bit
I was using a wrong syntax where I need to pass $maxDistance in the dictionary passed to the pos
Wrong
db.kol_sample.find({"pos":{$near:[22.1173487,88.0034742]},$maxDistance:100})
Correct
db.kol_sample.find({pos:{$near:[22.1173487,88.0034742],$maxDistance:1/111.2}})

Limit results of Mongodb find() query not working

I must be overlooking really simple, here's a sample of my db :
{ "_id" : ObjectId("545e7e45a69b6c5c0caeafcc"), "url" : "http://example.com/id=4314936", "name" : "product1", "retailer" : "123", "extra" : [ ], "timestamp" : 1415478797263, "processed" : 0 }
{ "_id" : ObjectId("545e7e45a69b6c5c0caeafcc"), "url" : "http://example.com/id=4314936", "name" : "product2", "retailer" : "123", "extra" : [ ], "timestamp" : 1415478797263, "processed" : 0 }
{ "_id" : ObjectId("545e7e45a69b6c5c0caeafcc"), "url" : "http://example.com/id=4314936", "name" : "product3", "retailer" : "123", "extra" : [ ], "timestamp" : 1415478797263, "processed" : 0 }
{ "_id" : ObjectId("545e7e45a69b6c5c0caeafcc"), "url" : "http://example.com/id=4314936", "name" : "product3", "retailer" : "123", "extra" : [ ], "timestamp" : 1415478797263, "processed" : 0 }
I'm trying to get 2 results only using :
db.products.find({timestamp:1415478797263,processed:0},{},{limit:2})
I have read and tried similar questions on Stack Overflow, so please keep that in mind before flagging as duplicate.
No matter what I try it gives me the full set... The strangest thing is that I'm using a similar find() elsewhere and it works. An experienced pair of eyes should be able to spot the problem, thanks in advance!!
use this query, does it work?
db.products.find({timestamp:1415478797263,processed:0}).limit(2)
now to use it with node js native driver you can do this
db.products.find({timestamp:1415478797263,processed:0}).limit(2).each(function(err, doc) {
console.dir(doc);
});
or in array form you can get this as
db.products.find({timestamp:1415478797263,processed:0}).limit(2).toArray(function(err, docs) {
console.log("Returned #" + docs.length + " documents");
})

how to use aggregation of mongodb

From the data as given below, I want to sum all Values fields.
Please let me know how can I do it using aggregation functionality of mongodb.
{"MetricRecord":
{ "SchemaVersion" : "0.12",
"Product": {
"ProductName" : "abc",
"ProductVersion": "7.5.0.1" ,
"ProductId" : "1234567890ABDFGH12345",
"InstanceId" : "12345BA32",
"InstanceName" : "1234SS123",
"SystemId" : "somehost.com"
},
"Tenant" : {
"CustomerId" : "222-555-124",
"ServiceCode": "xyzxyzxyz12345yyy"
},
"Metrics" : [
{
"ReportType" :[
{ "report" : "billing" },
],
"LogTime" : "2013-12-08T12:34:56:01Z" ,
"Type" : "AuthorizedUsers",
"SubType" : "registered",
"Value" : "125",
"UnitOfMeasure": "USD",
"Period" : {
"StartTime" : "2013-12-07T00:00:00:01Z",
"EndTime" : "2013-12-08T00:00:00:01Z"
}
},
{
"ReportType" :[
{ "report" : "billing" }
],
"LogTime" : "2013-12-08T12:34:56:01Z" ,
"Type" : "NumberOfTickets",
"SubType" : "resolved",
"Value" : "430",
"UnitOfMeasure": "USD",
"Period" : {
"StartTime" : "2013-12-07T00:00:00:01Z",
"EndTime" : "2013-12-08T00:00:00:01Z"
}
}
]
}
}
So, results which I expect from summation of values is 430+125 i.e. 555
Your document contains string value for MetricRecord.Metrics[index].Value field and i am not sure why are you trying to sum up the string values. if it is a typo and your document contains numerical values for MetricRecord.Metrics[index].Value field then you can try the following query
db.metrics.aggregate([
{$unwind:"$MetricRecord.Metrics"},
{$group:{_id:"$_id",sum:{$sum:"$MetricRecord.Metrics.Value"}}}
])
In the above document posted, if your value field is like
MetricRecord.Metrics[0].Value is 125(not "125")
MetricRecord.Metrics[1].Value is 430(not "430")
you will get the following output
{
"result" : [
{
"_id" : ObjectId("xxxxxxxxxxxxxxxxxxxxxxxx"),
"sum" : 555
}
],
"ok" : 1
}
The above sample query is composed assuming you have the default mongodb "_id" field and you are using a metrics collection. You have to manipulate the query as per you requirements.