Rethinkdb update nested object - updates

I have a document like below-
{
"badgeCount": {
"0e2f8e0c-2a18-499d-8e64-75b5d284e31a": 0 ,
"c0d07f5c-25ff-4829-8145-c33c0871f889": 0
} ,
"createdDate": Mon Oct 10 2016 19:25:10 GMT+00:00 ,
"deleted": false ,
"id": "1330c8b8-38a2-46e4-b93d-f7ff84a423ed",
"joinedUserIds":
[
"0e2f8e0c-2a18-499d-8e64-75b5d284e31a"
]
}
What I want to do is remove joinedUserId "0e2f8e0c-2a18-499d-8e64-75b5d284e31a" and update badgeCount by removing first property "0e2f8e0c-2a18-499d-8e64-75b5d284e31a": 0 in the same query.
I tried updating badgeCount like below-
r.db('mydb').table('discussion').filter({"id": "1330c8b8-38a2-46e4-b93d-f7ff84a423ed"})
.update(function(s){
return {
badgeCount: s('badgeCount').without("0e2f8e0c-2a18-499d-8e64-75b5d284e31a")
}
})
But it does not working. Not sure what I am missing.
Thanks
Anup

Rethink doesn't work like this. It doesn't rewrite object with update.
So, for this case you need to replace current object. And, btw, better to use get instead of filter, cause it's faster. There are doing the same in this situation 'cause id field is unique.
So, if you want to remove "0e2f8e0c-2a18-499d-8e64-75b5d284e31a" from badgeCount, you should use replace:
r.db('mydb').table('discussion').get("1330c8b8-38a2-46e4-b93d-f7ff84a423ed").replace(function(s){
return s.without({badgeCount : {"0e2f8e0c-2a18-499d-8e64-75b5d284e31a" : true}}).without("joinedUserIds").merge({
joinedUserIds : s("joinedUserIds").filter(function(id){
return id.ne("0e2f8e0c-2a18-499d-8e64-75b5d284e31a");
})
});
})

Related

How to set ignore null fields globally (JsonIgnoreCondition.WhenWritingNull)

i need to return an object fro mController in .NET core app 7.0
the object returned with null fields which I don't want to:
{
"field_1": "value",
"field_2": null,
"field_3": null
}
I wish to see
{
"field_1": "value"
}
I found out what i can add an attribute [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] to every filed and it will solve the problem, but i have hundreds of objects and dozens of fields in each of them.
Is it possible to set that property globally?
Somewhere like:
builder.Services.Configure<JsonOptions>(options =>
{
options.SerializerOptions... ???
});
I'm not using newstonsoft anymore, please help if you know how to use System.Text.Json.Serialization
Thank you
ok, i removed line:
//builder.Services.AddControllersWithJsonOptions();
and added a new one
builder.Services.AddControllers().AddJsonOptions(j =>
{
j.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
});
which solved "ignore null properties" problem

JSONPath filter property by value in object

How can I use the JSONPath filtering capabilities to query for a specific condition on a property on a subobject (without an array)?
Consider this JSON example:
{
"queue": {
"size": 13
}
}
I want to get a match if the .queue.size is greater than 0 and no match if it's equal to 0.
I tried using the following query but it does not work: $.queue[?(#.size>0)]. It is unclear to me why is that since the $.queue[size] does work returning value 13 correctly in this example, but if I include the filtering syntax I never get a match.
It looks like JSONPath expressions applies only to arrays. See here and here. Your query $.queue[?(#.size>0)] works for:
{
"queue": [{
"size": 13
},
{
"size": 10
}]
}
a little trick could be:
$..queue[?(#.size>0)]
The only downside is that it will change every object in the subtrees if it matches the criteria. not only at the first level.

JSONPath issu returning value using a filter

I have the following json, for which I'm having trouble selecting one of the values using a filter. Specifically, I want to return "POS_Undeclared"
{"wd:Program_of_Study_Reference": {
"wd:ID": [
{
"#wd:type": "WID",
"$": "123456789abcde"
},
{
"#wd:type": "Program_of_Study_ID",
"$": "POS_Undeclared"
}
]
}
}
This jsonpath $.wd:Program_of_Study_Reference.wd:ID[1].$ gives me what I need, but I cannot count on the ordering to be consistent or even exist, hence cannot use [1] .
I cannot seem to get a filter like #wd:type == Program_of_Study_ID to work. I'm guessing it is the # and/or the : goofing up my syntax.
How can I filter to get the value POS_Undeclared ?
Try using
$.wd:Program_of_Study_Reference.*[?(#['#wd:type'] == "Program_of_Study_ID")].$

Access of json object variable returned from MongoDB query fails

I'm trying to create a variable from an aggregate query in MongoDB and then use it to make another query.
var test_var = db.d_snapshot4.aggregate([{$group : {_id: null,
max_snapshot_date: {$max:"$snapshot_date"},
max_snapshot_date_str:
{$max:"$snapshot_date_str"}
}
}
]);
The content of test_var is as follows:
{
"_id": null,
"max_snapshot_date": ISODate("2020-05-31T00:00:00.000Z"),
"max_snapshot_date_str": "20200531"
}
But when I try to see the result of test_var.max_snapshot_date I get nothing back.
I need to use the variable as follows:
db.d_snapshot4.aggregate([{$match: {"snapshot_date": {$gte: test_var.max_snapshot_date } }}]);
Any suggestions?
Thanks,
Dina
Did you try with test_var["max_snapshot_date"]? Or do you get any error?

Mongo query to get comma separated value

I have query which is traversing only in forward direction.
example:
{
"orderStatus": "SUBMITTED",
"orderNumber": "785654",
"orderLine": [
{
"lineNumber": "E1000",
**"trackingnumber": "12345,67890",**
"lineStatus": "IN-PROGRESS",
"lineStatusCode": 50
}
],
"accountNumber": 9076
}
find({'orderLine.trackingNumber' : { $regex: "^12345.*"} })**
When I use the above query I get the entire document. But I want to fetch the document when I search with 67890 value as well
At any part of time I will be always querying with single tracking number only.
12345 or 67890 Either with 12345 or 67890. There are chances tracking number value can extend it's value 12345,56789,01234,56678.
I need to pull the whole document no matter what the tracking number is in whatever position.
OUTPUT
should be whole document
{
"orderStatus": "SUBMITTED",
"orderNumber": "785654",
"orderLine": [
{
"lineNumber": "E1000",
"trackingnumber": "12345,67890",
"lineStatus": "IN-PROGRESS",
"lineStatusCode": 50
}
],
"accountNumber": 9076
}
Also I have done indexing for trackingNumber field. Need help here. Thanks in advance.
Following will search with either 12345 or 67890. It is similar to like condition
find({'orderLine.trackingNumber' : { $regex: /12345/} })
find({'orderLine.trackingNumber' : { $regex: /67890/} })
There's also an alternative way to do this
Create a text index
db.order.createIndex({'orderLine.trackingnumber':"text"})
You can make use of this index to search the value from trackingnumber field
db.order.find({$text:{$search:'12345'}})
--
db.order.find({$text:{$search:'67890'}})
--
//Do take note that you can't search using few in between characters
//like the following query won't give any result..
db.order.find({$text:{$search:'6789'}}) //have purposefully removed 0
To further understand how $text searches work, please go through the following link.