{
"customer" : {
"addresses" : {
"address" : [
{
"city" : "bang",
"streetNo" : 123
},
{
"city" : "hyd",
"streetNo" : 321
}
]
},
"custId" : "123"
}
}
and i want to read a document from address array, which "city" : "bang" so how to read??
i used positional and $elemMatch operator but it is selecting all documents from array but not particular document of a array then what i will do
Then you didn't use the projection operator correctly.
Maybe you should provide the actual query that you used, and the result you got. Include the result you expected. It's very hard to guess what might be going wrong when you don't provide any details.
for eg:
Thanks
{
"customer" : {
"addresses" : {
"address" : [
{
"city" : "bang",
"streetNo" : 123
},
{
"city" : "hyd",
"streetNo" : 321
}
]
},
"custId" : "123"
}
}
My question is, from address array, i want to select a particular document which "city" : "bang", and "streetNo" : 123 ,what is the query??
My query is :
db.customer.find({"customer. addresses.address":{"$ elemMatch":{"city" : "bang", "streetNo" : 123}}})
and i got result,
{
"customer" : {
"addresses" : {
"address" : [
{
"city" : "bang",
"streetNo" : 123
},
{
"city" : "hyd",
"streetNo" : 321
}
]
},
"custId" : "123"
}
}
but my expected result is
{
"customer" : {
"addresses" : {
"address" : [
{
"city" : "bang",
"streetNo" : 123
}
]
},
"custId" : "123"
}
}
You didn't pass in any projection document. That's the second document/argument passed to find.
You are selecting based on a single address, but read the two links I sent initially - you can specify exactly which element of the addresses.address array you want returned either with "$" (because you matched on it) or with $elemMatch (which you can use even if you selected on a different criteria than the address).
In this collection example, I am want to check whether city = Chennai and street no : 321 exist or not using SQL query. Is it possible?
what is the equivalent SQL syntax for $elemMatch
"addresses" : {
"address" : [
{
"city" : "bang",
"streetNo" : 123
},
{
"city" : "hyd",
"streetNo" : 321
}
Please help.
I'm sorry, but I'm very confused by your request for "SQL equivalent" to $elemMatch syntax.
In SQL there isn't a concept of a single field (column) having multiple values, aka being an array, the reason $elemMatch exists in MongoDB is because a field is not limited to a single value but can be an array (that is, have multiple values).
Can you explain what exactly you are trying to do? Do you really mean SQL here? Are you using some SQL front end which is translating your SQL into MongoDB query language?
댓글 없음:
댓글 쓰기