last day (15 days later) » 

9:19 PM
1
A: Adding Node to exisitng documents mongodb

nimrod serokOne option is: Use $exists instead of $exist Use pipeline in order to be able to $set the value from another fields' value db.studentsPrimaryDetails.updateMany( {"address_details.communication_address": {$exists: false}}, [{$set: {address_details: {communication_address: $address_details}}]...

 
it worked but not exactly what I expected. Maybe I was not clear to explain. Please check I updated desired output
 
Is this what you wanted?
 
Yes, but But I see two address fields basically need communication_details is new node under which all country details will be something like second "address_details": { "communication_address": { the query I tried inserted wrong details anyway to $uset it?
 
Try this :)
 
No luck i did try nothing work's
 
9:19 PM
Please look at the playground example. Is this what you want? If so, we can work on your data to match it
 
I did try your playground example. You are inserting data but at the wrong position please check JSON at desired output . Pasting link in question
"address_details": {
  "address_line1": "",
  "address_line2": "",
  "city": {
    "city_id": ObjectId("6230139f683f8fb7bd21f1a9"),
    "city_name": "Akodia"
  },
  "communication_address": {
    "country": {
      "country_code": "IN",
      "country_id": ObjectId("623012f9683f8fb7bd213c36")
    }
  },
Thsi is what your query is doing
What i want is
```
"address_details": {
"communication_address": {
"country": {
"country_id": {
"$oid": "623012f9683f8fb7bd213c36"
},
"country_code": "IN"
},
"state": {
"state_id": {
"$oid": "623013d9683f8fb7bd2380ef"
},
"state_code": "AP"
},
"city": {
"city_id": {
"$oid": "6230139f683f8fb7bd21ebc8"
},
"city_name": "Akasahebpet"
},
"address_line1": "jjkjk",
"address_line2": "uiui",
"pincode": "989898"
}
},```
 
Current result is:
[
{
"_id": ObjectId("62a8ad644035e93c7ff24607"),
"address_details": {
"address_line1": "",
"address_line2": "",
"city": {
"city_id": ObjectId("6230139f683f8fb7bd21f1a9"),
"city_name": "Akodia"
},
"communication_address": {
"address_line1": "",
"address_line2": "",
"city": {
"city_id": ObjectId("6230139f683f8fb7bd21f1a9"),
"city_name": "Akodia"
},
"country": {
"country_code": "IN",
"country_id": ObjectId("623012f9683f8fb7bd213c36")
},
"pincode": "",
"state": {
"state_code": "MP",
"state_id": ObjectId("623013d9683f8fb7bd2381c4")
identical to your requested results
 
"address_details": {
"address_line1": "",
"address_line2": "",
"city": {
"city_id": ObjectId("6230139f683f8fb7bd21f1a9"),
"city_name": "Akodia"
},
"communication_address": {
"address_line1": "",
"address_line2": "",
Above one is repeated
I want
"address_details": {
"communication_address": {

Then inside this city and all address details node
 
Let me see
Check this out
I updated the answer accordingly
Are you here?
 
9:41 PM
Yes
Give me sec
Output seems correct but on DB running query didn't update
Output seems correct on Playground
 
What is the difference between db and playground? What is the db version?
 
{'address_details.communication_address': {$exists: true}}
I did this and only 12 result came documents haveing communication address

{'address_details.communication_address': {$exists: false}}
Return 2014
5.0.9 Enterprise hosted DB
MongoDB version 5.0.9 on playground
 
Show me one of the false
 
This one is false
And my mistake
You did it
db.studentsPrimaryDetails.updateMany({
"address_details.communication_address": {
"$exists": false
}
},
[
{
$set: {
"address_detailsB": {
communication_address: "$address_details"
}
}
},
{
$set: {
"address_details": "$address_detailsB",
"address_detailsB": "$$REMOVE"
}
}
])
 
Is this working for you?
 
9:49 PM
problem with update
Added updateMany and it worked
Yes
It working thanks a lot
 
Ahhh
Sure
Happy to help
 
{'address_details.communication_address': {$exists: false}}
This result in 0
 
You can delete the comments from the answer
I updated it
 
Great
Yes one more thing .. This query added blank also can I delete that

address_details
Object

Object

communication_address
Object
 

last day (15 days later) »