How can I search the collections in the collection in mongodb with c# .net core
{
"isbn": "123-456-222",
"author": {
"_id": 1,
"lastname": "Doe",
"firstname": "Jane"
},
"editor": [
{
"_id": 1,
"lastname": "Smith",
"firstname": "Jane"
},
{
"_id": 2,
"lastname": "Lopez",
"firstname": "Jennifer"
}
],
"title": "The Ultimate Database Study Guide",
"category": [
"Non-Fiction",
"Technology"
]
}
I am using mongo db database and I am using mongo driver library in .net core. I can search by isbn, but I want to find all books with editor firstName:Jane, LastName:Smith and firstName=Jennifer, lastname:Lopez, how can I do that
CodePudding user response:
I don't know C#/.NET
, but here's a query that you may be able to translate to retrieve the documents you want.
db.collection.find({
"$and": [
{
"editor": {
"$elemMatch": {
"lastname": "Smith",
"firstname": "Jane"
}
}
},
{
"editor": {
"$elemMatch": {
"lastname": "Lopez",
"firstname": "Jennifer"
}
}
}
]
})
Solution 2: With dot notation
MongoDB query
db.collection.find({
"$and": [
{
"editor.lastname": "Smith",
"editor.firstname": "Jane"
},
{
"editor.lastname": "Lopez",
"editor.firstname": "Jennifer"
}
]
})