I have a collection with elements like this:
{
"_id": {
"$oid": "56d61033a378eccde8a8354f"
},
"id": "10021-2015-ENFO",
"certificate_number": 9278806,
"business_name": "ATLIXCO DELI GROCERY INC.",
"date": "Feb 20 2015",
"result": "No Violation Issued",
"sector": "Cigarette Retail Dealer - 127",
"address": {
"city": "RIDGEWOOD",
"zip": 11385,
"street": "MENAHAN ST",
"number": 1712
}
}
What I want is to add _id
for each address
. This is one time operation and I am doing it for researching/testing purpose.
I think that I can split the task into the following steps:
- get all unique addresses and create separate collection with them, assigning
_id
for each record (as I have read will add it if not specified) - make a join by all
address
fields in order to insert the corresponding_id
for each embedded document in the source collection
I select the documents like like this:
db.ci.find({}, {"address":1, "_id":0});
but I fail to make a distinct from it using the Distinct
function or foreach
. I try to use aggregate
, too but did not make it work.
Could anyone give me some tips?
I am using Ubuntu 20.04, mongodb 4.2.7, vs code with mongodb extension.