Execute multiple queries at same time, if the all the queries are valid then only I should get the response in MongoDB - mongodb

I am trying to execute 2 queries at the same time, for example, refer below:
db.getCollection('Test').find({'color':'red'},{'color':'yellow'});
Assume that color red is present in the one collection and yellow is present in another collection, but I am getting the response only from the first query.
Expectation:
1.If both the queries are present in any of the collection I should get both
responses.
2.If anyone of the query is invalid or element is not present in the collection,
I should not get any response.
Thanks in advance

Since you want to match existence of both values, use $all operator:
db.getCollection('Test').find({color:{$all:["red","yellow"]}})
Edit
I managed to get your output, but I think this could be simplified. I thought about different options and ended up in this query:
db.colors.aggregate([{
$facet:{
cond1:[{$match:{color:"red"}}],
cond2:[{$match:{color:"yellow"}}]
}},
{$project:{match1: "$cond1", match2:"$cond2" ,size1:{$size: "$cond1"},
size2:{$size: "$cond2"}}},
{$project:{result:{$cond:[{$and:[{$gte:["$size1",1]},{$gte:
["$size2",1]}]},{$concatArrays:["$match1","$match2"]},[]]}}}
])

I think there is something wrong with your question.
I think you are looking for something like this db.getCollection('Test').find({color: {$in: ['red','yellow']}});
I hope this will help.

Related

How can Mongo query specify that two conditions in an array are satisfied at the same time?

For example, the doc is
{'id':'1',
'tag':[{'tag_a':'A',
'score':'10'},
{'tag_b':'B',
'score':'0'}
]
}
I want get the doc that satisfied tag_b get 10 points in my collection. Obviously, I should not get this doc. However, when I use the query below
{$and:[{'tag.tag_b':'B','tag.score':'10'}]}
This doc will appear in the results.
How can I avoid this situation. Thanks!

MONGODB: $in operator not matching any record

community!
I am in a weird situation. The direct equally check returns result, but when using $in I am not getting any records.
db.getCollection("voter").find({"id":{$in:["db1eefc5-09ad-4d4f-a31a-db63d8261913"]}})
db.voter.find({"id":{$in:["db1eefc5-09ad-4d4f-a31a-db63d8261913"]}})
Doesn't return anything.
db.voter.find({id: "db1eefc5-09ad-4d4f-a31a-db63d8261913"})
Returns the desired record.
Being more of a fullstack developer, I don't know what's happening in-depth, but I am sure that both things shall work ideally which is not the case here.
Extra info:
I have defined hashed unique indexes on id.
Thanks.
The problem is pretty simple:
On the first screen you're running your query against admin database
while second query gets executed against crmadmin db

Mongodb Query with conjoined data points

I'm trying to create a query that can pull this information from my database using just the type score when it's less than 70 and when it's of type exam:
{"_id":0,"name":"aimee Zank",
"scores":[{"score":1.463179736705023,"type":"exam"},
{"score":11.78273309957772,"type":"quiz"},{"score":35.8740349954354,"type":"homework"}]}
I'm not getting any errors and it's turning up blank. Here's the query I'm trying to use:
db.students.find({"scores":[{"score":{$lt:70},"type":"exam"}]}).pretty
Any help would be appreciated :)
That is not how you match elements inside array. Use $elemMatch :
db.students.find({"scores":{$elemMatch:{score:{$lt:70},type:"exam"}}})
Update:
OP marked the post as "not answered" and then come with twisted requirement. Going forward, make sure you post a seperate question for your entirely new requirements or state your problem properly, dont extend it as the time flies. However, here's the filter query using aggregation framework:
db.collection.aggregate([
{$addFields:{scores:{$filter:{
input:"$scores",
as:"score",
cond:{$and:[{$eq:["$$score.type","exam"]},{$lt:["$$score.score",70]}]}
}}}}
])
This filters all exams that have score less than 70.

Mongo DB search based on multiple conditions

I am trying to search based on multiple conditions which works but the problem is that does not behave like this.
Assuming i have a search query like
Orders.find({$or: {"status":{"$in":["open", "closed"]},"paymentStatus":{"$in":["unpaid"]}}}
)
and i add another filter parameter like approvalStatus it does not leave the previously found items but rather it treats the query like an AND that will return an empty collection of items if one of the queries does not match.
How can i write a query that regardless of what is passed into it, it will retain previously found items even if there is no record in one of the conditions.
like a simple OR query in sql
I hope i explained this well enough
Using $or here is the right approach, but its value needs to be an array of query expressions, not an object.
So your query should look something like this instead:
Orders.find({$or: [
{"status": {"$in": ["open", "closed"]}},
{"paymentStatus": {"$in": ["unpaid"]}},
{"approvalStatus": {"$in": ["approved"]}}
]})

How do I make a mongo query for something that is not in a subdocument array of heterodox size?

I have a mongodb collection full of 65k+ documents, each one with a properties named site_histories. The value of it is an array that might be empty, or might not be. If it is not empty, it will have one or more objects similar to this:
"site_histories" : "[{\"site_id\":\"129373\",\"accepted\":\"1\",\"rejected\":\"0\",\"pending\":\"0\",\"user_id\":\"12743\"}]"
I need to make a query that will look for every instance in the collection of a document that does not have a given user_id.
I'm pretty new to Mongo, so I was trying to make a query that would find every instance that does have the given user_id, which I was then planning on adding a "$ne" to, but even that didn't work. This is the query I was using that didn't work:
db.test.find({site_histories: { $elemMatch: {user_id: '12743\' }}})
So can anyone tell me why this query didn't work? And can anyone help me format a query that will do what I need the final query to do?
If your site_histories really is an array, it should be as simple as doing:
db.test.find({"site_histories.user_id": "12743"})
That looks in all the elements of the array.
However, I'm a bit scared of all those backslashes. If site_histories is a string, that won't work. It would mean that the schema is poorly designed, you'd maybe try with $regex