In a document like this:
[
{
a: "",
b: 1,
"createdAccounts": {
"test@gmail.com": {
"id": ["a", "b", "c"]
}
}
}
]
I’m querying an email as:
const email = "test@gmail.com"
let account = await db.collection("users").aggregate(
[
{
"$project": {
"createdAccounts": {
"$objectToArray": "$createdAccounts"
}
}
},
{
"$match": {
"createdAccounts.k": email
}
},
{
"$project": {
"createdAccounts": {
"$arrayToObject": "$createdAccounts"
}
}
}
]).toArray().then(results =>
{
const document = results[0];
if (document && document.createdAccounts)
return document.createdAccounts[email];
return null;
})
I would like to ask two questions:
1-
There’s a “better way” to query the email?
I’m using aggregate because the searched key can contain a dot ( .)
in its path
and in this case, this:
const email = "test@gmail.com"
let account = await db.collection("users").findOne({ "createdAccounts.email": email });
wouldnt work.
2-
Using the aggregate query I get as response:
"test@gmail.com": {
"id": ["a", "b", "c"]
}
Suppose i need to modify the id
array, as its inside a key containing a dot ( .
)
i couldn’t find how to update it.
Also, the update method will concatenate the array or overwrite it?