MONGODB: $in operator not matching any record - mongodb

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

Related

Execute multiple queries at same time, if the all the queries are valid then only I should get the response in 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.

Meteor Mongodb first object array not selectable with dot notation

I have tried and tried on Meteor and on Robomongo (Mongodb) to select objects with dot notation.
I would like to be able to filter team.0.wageringStats.wageringStraightSpread objects (sometimes subjects can be fields or arrays - thats another issue)
In the first image I can select team.wageringStats.wageringStraightSpread and get back all the subOjects of team (team has siblings not shown in images)
The second image I tried team.0.wageringStats.wageringStraightSpread and I get no fields.
Lastly i tried team.[0].wageringStats.wageringStraightSpread and
team[0].wageringStats.wageringStraightSpread and get the same result : 0 fields
I am at a loss and would like some help. Thank you
I am not sure what you are trying to do now? Because in your first command, you already have a list of team that match your criteria and then, put it into the loop of meteor to process. Why do you need to find only the first one ? By the way, in order to select the nth of the result set in mongodb, you will need something like skip and limit
db.collections.find({'team.wageringStats.wageringStraightSpread':1}).limit(1).skip(0)
(in skip, you need to pass the offset you need to reach to)
Also, if you only care about the first one, findOne is the one you need to do the query
db.collections.findOne({'team.wageringStats.wageringStraightSpread':1})
Be aware that the syntax of mongodb and meteor for querying is a bit different

Optimizing delete queries in MongoDB using the "justOne" keyword

I was wondering if the justOne keyword helps the speed of a remove query even if you are querying by a unique field (i.e. there is only one instance of the document).
For instance using pymongo:
for id in list_of_ids:
db.remove({"_id":id})
Does it still speed up the query if I use the justOne argument?
for id in list_of_ids:
db.remove({"_id":id},justOne=True)
It wouldn't make sense, but I don't know if mongo is smart enough to know that this is the unique id so of course there will only be one.
J
No, this will not speed up the query. First of all, Mongo will retrieve all documents, that match your condition and then perform one delete. Since Mongo will retrieve just one document, so - no speedup there.

What is the fastest way to see when the last update to MongoDB was made

I'm writing a long-polling script to check for new documents in my mongo collection and the only way I know of checking to see whether or not changes have been made in the past iteration of my loop is to do a query getting the last document's ID and parsing out the timestamp in that ID and seeing if it's greater than the timestamp left since I last made a request.
Is there some kind of approach that doesn't involve making a query every time or something that makes that query the fastest?
I was thinking a query like this:
db.chat.find().sort({_id:-1}).limit(1);
But it would be using the PHP driver.
The fastest way will be creating indexes on timestamp field.
Creating index:
db.posts.ensureIndex( { timestamp : 1 } )
Optimizes this query:
db.posts.find().sort( { timestamp : -1 } )
findOne give you only one the last timestamp.
nice to help you.
#Zagorulkin Your suggestion is surely going to help in the required scenario. However i don't think so sort() works with findOne().

Sphinx deleted documents

I have this problem for a long time, and can't find a solution.
I guess this might be something everybodys faced using Sphinx, but I cnanot get any
usefull information.
I have one index, and a delta.
I queried in a php module both indexes, and then show the results.
For each ID in the result, I create an object for the model, and dsiplay main data for
that model.
I delete one document from the database, phisically.
When I query the index, the ID for this deleted document is still there (in the sphinx
result set).
Maybe I can detect this by code, and avoid showing it, but the result set sphinx gaves me
as result is wrong. xxx total_found, when really is xxx-1.
For example, Sphinx gaves me the first 20 results, but one of this 20 results doesn't
exists anymore, so I have to show only 19 results.
I re-index the main index once per day, and the delta index, each 5 minutes.
Is there a solution for this??
Thanks in advance!!
What I've done in my Ruby Sphinx adapter, Thinking Sphinx, is to track when records are deleted, and update a boolean attribute for the records in the main index (I call it sphinx_deleted). Then, whenever I search, I filter on values where sphinx_deleted is 0. In the sql_query configuration, I have the explicit attribute as follows:
SELECT fields, more_fields, 0 as sphinx_deleted FROM table
And of course there's the attribute definition as well.
sql_attr_bool = sphinx_deleted
Keep in mind that these updates to attributes (using the Sphinx API) are only stored in memory - the underlying index files aren't changed, so if you restart Sphinx, you'll lose this knowledge, unless you do a full index as well.
This is a bit of work, but it will ensure your result count and pagination will work neatly.
Maybe this fits better to my needs, but involves changing the database.
http://sphinxsearch.com/docs/current.html#conf-sql-query-killlist
I suppose you could ask for maybe 25 results from sphinx and then when you get the full data from your DB just have a limit 20 on the query.