query is not notworking - mongodb

I am using mongodb as my database and using scala for developing and i am new to this.
Now i have a problem in updating the database using a query
collectionProfile.update(BSONDocument("handle"->handle,"active"->true), BSONDocument("$set"->BSONDocument("messages.received"->BSONDocument("isRead"->true))))
and here is my collection
{
"_id" : ObjectId("5877969d1300005801327fa8"),
"handle" : "sd3423dwwrewer342",
"active" : true,
"messages" : {
"received" : [
{
"id" : "u-bf130421-418b-41a5-8965-0e7691b9412a",
"by" : "S_0ECB8108_7771_4701_9813_D7D360B994AC",
"to" : "J_04ABD48A_28D2_44CD_9D05_76C709E50724",
"subject" : "hi",
"content" : "hello",
"time" : "2017-01-12T20:16:25.268",
"isRead" : false
}
]
}
}
here i am trying to update the isRead variable true but it is not working can anyone help me with this query.

Related

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

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 }
})

Mongo query to check whether subobject exist or not

I have a collection invoice with subobject address.'invoiceId' is the key of main object(Invoice) and '130704432ST'is key for address(address subobject is saved as Map(key,value) pair).I need to check whether this subobject exists or not, if exist I need to update this. So while querying it gives no object found for below query. I am using spring Mongotemplate query in my application. So my query looks like below.
Kindly share if you find any issues with this query and also help me with Mongo query to execute in Robo 3T 1.2 for below criteria.
Query: { "_id" : { "$java" : 144068830 }, "addresses.144068830SF" : { "$exists" : true } }, Fields: { }, Sort: { }
{
"_id" : {
"invoiceId" : 130704432
},
"destStoreNbr" : 82,
"invoiceTypeCd" : "M",
"countryCode" : "US",
"invoiceNumber" : "0000000086 ",
"totalAmt" : 1038.76,
"invoiceId" : 130704432
"addresses" : {
"130704432ST" : {
"streetAddress4" : "",
"name" : "kEN-MART",
"streetAddress3" : "",
"invAddrTypeCode" : "ST",
"streetAddress2" : "",
"zipCode" : "310310",
"cityName" : "ATLANTA",
"countryCode" : "US",
"stateCode" : "AL"
}
}

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!!

find and replace string in all mongodb collections

I have a mongodb database named "eagle" and am trying to replace all email records of "blue#domain.com" with "pink#domain.com" (within collection "all collections".
db.eagle.find({}).forEach(function(e,i) {
e.email=e.email.replace("//blue#domain.com","//pink#domain.com");
db.eagle.save(e);
});
I am very new to mongodb...so I'm not even sure that I'm in "eagle" or "falcon"....I just run mongo. This query doesn't do anything.
Here is what my Communications object looks like:
{
"_id" : ObjectId("redacted"),
"timestamp" : ISODate("2016-08-03T15:08:07.000Z"),
"thread_index" : "",
"updated_at" : ISODate("2016-09-01T17:49:31.401Z"),
"from" : {
"username" : "None",
"name" : "Pinky Jones",
"email" : "blue#domain.com"
},
"to" : {
"username" : "redude",
"name" : "Red Baron",
"email" : "red#domain.com"
},
"created_at" : ISODate("2016-09-01T17:49:31.401Z"),
}
Use findAndModify instead find
Documentación findAndModify

How to generate mongodb object id for a sub-document on insert

Inserting a document that also has sub-documents. I am doing this through cli. How do I make mongodb generate a object id for the Alerts sub-document record? I want something like the following, but this does not work from cli.
db.user.insert(
{
"Email" : "andy+5#domain.com",
"Domain" : "other",
"Type" : "local",
"FName" : "Andy",
"Alerts" : [
{
"_id" : new ObjectId(),
"Keyword" : "sales",
"Frequency" : {
"Type" : "daily"
},
"IsActive" : true
},
{
"_id" : new ObjectId(),
"Keyword" : "customer service",
"Frequency" : {
"Type" : "daily"
},
"IsActive" : true
}
]
}
)
You can run that exact script on the console on robomongo, it would execute just fine. Don't do it on add or edit dialog.