I have a document like this:
{
"_id": "5ffc130e9fb31b26162e0bad",
"results": [
{
"customer": {
"display_name": "Manno Dispensary - first",
"ext_acct_id": "267"
}
},
{
"customer": {
"display_name": "Manno Dispensary - second",
"ext_acct_id": "262"
}
},
{
"customer": {
"display_name": "Kako Dispensary - first",
"ext_acct_id": "261"
}
},
{
"customer": {
"display_name": "Kako Dispensary - second",
"ext_acct_id": "263"
}
}
]
}
I want to write a MongoDB query which does a regex search on "customer.display_name"
and returns all those documents in results that satisfies this criteria.
I have written this query till now, and it returns me the desired output, but the problem is, it is only retuning one document inside results, Am I missing anything in this?
my desired output:
{
"_id": "5ffc130e9fb31b26162e0bad",
"results": [
{
"customer": {
"display_name": "Manno Dispensary - first",
"ext_acct_id": "267"
}
},
{
"customer": {
"display_name": "Manno Dispensary - second",
"ext_acct_id": "262"
}
}
]
}
What I am actually getting:
{
"_id": "5ffc130e9fb31b26162e0bad",
"results": [
{
"customer": {
"display_name": "Manno Dispensary - first",
"ext_acct_id": "267"
}
}
]
}
this is the query which I have written to fetch all customers which contain "Manno" in their customer name.
My collection name is Order(mongoose), search="Manno"
Order.find({
results: {
$elemMatch : {
"customer.display_name": {$regex: search}
}
}
},{
results: {
$elemMatch : {
"customer.display_name": {$regex: search}
}
}
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…