How to push new key and value in JSON array in mongodb? - json

How can I push new key and value in JSON array?
I tried I used push keyword in update query but I got a different output. I used:
db.users.updateOne({"name":"viki"},{$push{"address.district":"thambaram"}})
I have this document:
{ "_id" : ObjectId("58934f10c7592b1494fd9a4d"), "name" : "viki", "age" : 100, "subject" : [ "c", "node.js", "java" ], "address" : { "city" : "chennai", "state" : "tamilnadu", "pincode" : "123" } }
I want to add "district":"thambaram" in address json array
I need like:
{ "_id" : ObjectId("58934f10c7592b1494fd9a4d"), "name" : "viki", "age" : 100, "subject" : [ "c", "node.js", "java" ], "address" : { "city" : "chennai", "state" : "tamilnadu", "pincode" : "123","district":"thambaram"} }

Use $set
db.users.updateOne({"name":"viki"},{$set:{"address.district":"thambaram"}})
This should work.
The $push operator appends a specified value to an array. In your case you should use $set

Related

How to add number as firebase realtime database child without converting to array?

I want to make a DB structure like the following:
But it just converts the whole object to an array.
Here's what I get when I export this JSON.
[ null, {
"oPNfOlBcS4gl3rvZ4CIme9MDk0p1" : {
"added_at" : 1647966583316,
"city" : "Test",
"country" : "Test",
"name" : "Test",
"state" : "Test",
"uid" : "oPNfOlBcS4gl3rvZ4CIme9MDk0p1"
}
}, {
"oPNfOlBcS4gl3rvZ4CIme9MDk0p1" : {
"added_at" : 1647966583316,
"city" : "Test",
"country" : "Test",
"name" : "Test",
"state" : "Test",
"uid" : "oPNfOlBcS4gl3rvZ4CIme9MDk0p1"
}
} ]
I tried keeping the first child as 1 and the second as "first" (or some random string) and exported JSON and here's what I got...
{
"1" : {
"oPNfOlBcS4gl3rvZ4CIme9MDk0p1" : {
"added_at" : 1647966583316,
"city" : "Test",
"country" : "Test",
"name" : "Test",
"state" : "Test",
"uid" : "oPNfOlBcS4gl3rvZ4CIme9MDk0p1"
}
},
"first" : {
"oPNfOlBcS4gl3rvZ4CIme9MDk0p1" : {
"added_at" : 1647966583316,
"city" : "Test",
"country" : "Test",
"name" : "Test",
"state" : "Test",
"uid" : "oPNfOlBcS4gl3rvZ4CIme9MDk0p1"
}
}
}
I want something similar, but with children like "1", "2" instead.
Is it possible? Or using children like "first", "second" is the only solution right now?
When you read a node with sequential numeric keys (as in your screenshot and first JSON example), the Firebase SDK (and REST) API) automatically coerce that data to an array. There is no way to configure this behavior.
If you don't want the array coercion you should use non-numeric keys. My common approach is to prefix each key with a short string, like "key1", "key2", etc.
Also see: Best Practices: Arrays in Firebase.

F# - Compare 2 JsonValue'

I have 2 Json values that are similar, but there are some differences.
Json1:
{
"id": "1",
"people" : [
{
"Id" : 1421,
"Name" : "Jackson",
"Age" : 21,
"Status" : "Available"
},
{
"Id" : 5916,
"Name" : "Steven",
"Age" : 22,
"Status" : "Available"
}
],
"totalRecords" : 2
}
Json2:
{
"id": "1",
"people" : [
{
"Id" : 1421,
"Name" : "Jackson",
"Age" : 21,
"Status" : "Available"
},
{
"Id" : 5916,
"Name" : "Steven",
"Age" : 22,
"Status" : "Unavailable"
},
{
"Id" : 1337,
"Name" : "Alice",
"Age" : 19,
"Status" : "Available"
}
],
"totalRecords" : 3
}
I'd like to know if there's a way to compare the two Jsonvalues. At the moment I de-serialize the data into a type and then use the Id's and the status' to see if anythings changed. I then pick out the parts that are different (In the example it'd be Steven and Alice) and add them to a sequence for later.
I'd like to reverse a few of the steps. I'd like too compare the json, find the differences, deserialize them and then add them to the sequence, or add them to the sequence then de-serialize the whole sequence. Either way, same result.
Any ideas?

Push data in json array MongoDB

I have the following structure in my mondoDB:
"source" : [
{
"source_id" : "800000021",
"source" : "Test1"
}
]
"state" : "AM",
"owner_phone" : "0",
"latitude" : 0,
"leedon_activated" : "True",
"zip_code" : "12345",
"fax" : "0",
I want to add another json to the source array for the following resut:
"source" : [
{
"source_id" : "800000021",
"source" : "Test1"
},
{
"source_id" : "800000022",
"source" : "Test2"
}
]
"state" : "AM",
"owner_phone" : "0",
"latitude" : 0,
"leedon_activated" : "True",
"zip_code" : "12345",
"fax" : "0"
What will be MongoDB query for it?
Read about $push here
Your code will something like this
yourmodelname.update({ /* some conditions to updating files */ },{$push: {"source": {"source_id" : "213123", "source" : "asdasd"}}})

angular js json response grabbing of different matches (pattern to match all type of response)

the response is like the following format
{
"human_man" :
[
{"id" : "12345", "value" : "4567"},
{ "id" : "0000", "value" : "qwer"}
],
"human_woman" :
[
{"id" : "5454", "value" : "6565"},
{ "id" : "7878", "value" : "884"}
],
............................................
}
I want to catch the response for all matches , meaning....
if I use,
response.human_man ::: i would be catching--> [ {"id" : "12345", "value" : "4567"}, { "id" : "0000", "value" : "qwer"}]
if I use,
response.human_woman ::: i would be catching--> [{"id" : "5454", "value" : "6565"}, { "id" : "7878", "value" : "884"}]
so I want to know how to catch the response the type is
response.human_* (it should catch response.human_man & response.human_woman)
hope you guys understood the question.. :)
fast replies would be appreciated
Try this :)
var object = {
"human_man" :
[
{"id" : "12345", "value" : "4567"},
{ "id" : "0000", "value" : "qwer"}
],
"human_woman" :
[
{"id" : "5454", "value" : "6565"},
{ "id" : "7878", "value" : "884"}
]
};
for(var key in object) {
if(/^human_/.test(key))
console.log(object[key]);
}

How to update a nested array value in mongodb?

I want update a array value that is nested within an array value: i.e. set
status = enabled
where alerts.id = 2
{
"_id" : ObjectId("5496a8ed49847b6cd7c7b350"),
"name" : "joe",
"locations" : [
{
"name": "my location",
"alerts" : [
{
"id" : 1,
"status" : null
},
{
"id" : 2,
"status" : null
}
]
}
]
}
I would have used the position $ character, but cannot use it twice in a statement - multi positional operators are not supported yet: https://jira.mongodb.org/browse/SERVER-831
How do I issue a statement to only update the status field of an alert matching an id of 2?
UPDATE
If I change the schema as follows:
{
"_id" : ObjectId("5496ab2149847b6cd7c7b352"),
"name" : "joe",
"locations" : {
"my location" : {
"alerts" : [
{
"id" : 1,
"status" : "enabled"
},
{
"id" : 2,
"status" : "enabled"
}
]
},
"my other location" : {
"alerts" : [
{
"id" : 3,
"status" : null
},
{
"id" : 4,
"status" : null
}
]
}
}
}
I can then use:
update({"locations.my location.alerts.id":1},{$set: {"locations.my location.alerts.$.status": "enabled"}});
Problem is I cannot create indexes on the alert id :-(
it may be better of modelled as such, specially if an index on location and,or alerts.id is needed.
{
"_id" : ObjectId("5496a8ed49847b6cd7c7b350"),
"name" : "joe",
"location" : "myLocation",
"alerts" : [{
"id" : 1,
"status" : null
},
{
"id" : 2,
"status" : null
}
]
}
{
"_id" : ObjectId("5496a8ed49847b6cd7c7b350"),
"name" : "joe",
"location" : "otherLocation",
"alerts" : [{
"id" : 1,
"status" : null
},
{
"id" : 2,
"status" : null
}
]
}
I think you are having a wrong tool for the job. What you have in your example is relational data and it's much easier to handle with relational database. So I would suggest to use SQL-database instead of mongo.
But if you really want to do it with mongo, then I guess the only option is to fetch the document and modify it and put it back.