MongoDb query and operation for multiple negative conditions - mongodb

I stuck at a query. Maybe i worked so long but could you help me to find which operator i should use for this step.
Example collection
[
{color: "red",size: "m"},
{color: "red",size: "s"},
{color: "blue",size: "m"},
{color: "blue",size: "s"}
]
I want to query document which are not red and not m.
db.foo.find({color:{$ne:'red'}, size:{$ne:'m'}}) returns only [{color:'blue', size:'s'}] but i want to get [{color:'red', size:'s'},{color:'blue', size:'m'},{color:'blue', size:'s'}]
Thanks for help.

You are using the wrong filter. You just need to update filter query as,
{"$or":[{color:{$ne:'red'}}, {size:{$ne:'m'}}]}

Related

Random item from Algolia

How do I get random algolia item from it's index?
All of my items have:
objectID "POST#news#44.7704046#17.1900285"
name "News"
categories [ "cafe", "food", "establishment", "food" ]
_geoloc { lat: "44.7704046", lng: "17.1900285" }
I would like to optionally search by name, match 1 or all categories, geo location filtering with distance, and most importantly, I only want 1 RANDOM returned from Algolia.
I can't do client side random, because sometimes without filters I would get too many results back ( 10000 ), so I can't transfer that over the wire.
Please help
Hi #Djordje there are no real way to get a random result with Algolia though you you could use an attribute to randomise the results and only use the first item. See documentation [here][1]

How to sort results from nearest to farthest with $geoWithin conditional?

I have the following query to find all documents within particular bounds:
db.collection.find({
"location": {
"$geoWithin": {
"$box": [
[165.8694369, -52.61941849999999],
[-175.831536, -29.2313419]
]
}
}
});
Also I have the coordinates of center of this bounds: center_lat: -40.900557, center_lng: 174.885971.
Is it possible to sort result documents by nearest to farthest from the center point?
The $geoWithin operator does not return sorted results. As such, MongoDB can return $geoWithin queries more quickly than geospatial $near or $nearSphere queries, which sort results.
https://www.mongodb.com/docs/manual/reference/operator/query/geoWithin/

Mongodb geolocation boundaries search/query

I have a documents contains list of location "boxes" (square area). Each box is represented by 2 points (bottom-left or south-west, top-right or north-east).
Document, for example:
{
locations: [
[[bottom,left],[top,right]],
[[bottom,left],[top,right]],
[[bottom,left],[top,right]]
]
}
I'm using 2d index for those boundaries points.
My input is a specific location point [x,y] and I want to fetch all documents that have at list one box that this point is located in it.
Is there any geospatial operator I can use to do that?
How do I write this query?
You can use the box operator, see:
http://docs.mongodb.org/manual/reference/operator/query/box/#op._S_box with the following example taken directly from that page:
db.places.find( { loc : { $geoWithin : { $box :
[ [ 0 , 0 ] ,
[ 100 , 100 ] ] } } } )
It is worth noting that the 2d index is considered legacy. If you can convert to using GeoJSON and a 2dsphere index, then you can use the $geoWithin operator: see
http://docs.mongodb.org/manual/reference/operator/query/geoWithin/#op._S_geoWithin
GeoJSON has a number of other benefits, not least of which, is that it is easily transmitted and digested by web mapping apps such as OpenLayers or Leaflet.

How do I determine if a box intersects a mongoDB collection of boxes using geospacial method?

I have figured out how to make the query for the intersection, but can't figure out how to define the boxes in the database so that it returns me all boxes that intersect with my query parameter.
How do I accomplish this?
I have found this website to be most helpful. Just use their examples and everything should be clear. If not, consider turning that complex polygon into a square (which is also a polygin).
You can use these commands for a database called 'test' and collection called 'geo':
test.geo.insert( {_id: "Poly1", shape: {type: "Polygon", coordinates: [[ [1,2], [1,4], [4,4], [4,2], [1,2] ]] } });
test.geo.ensureIndex( {shape:"2dsphere"} );
geo.find( {shape: {$geoIntersects: {$geometry: {type: "Polygon", coordinates: [[ [2,4], [2,8], [3,8], [3,4], [2,4] ]] }}}} )
This particular example has the polygons intersecting on column 4. I have not yet tested all possible combinations but from the ones I tested, it does work.

How do you do an AND query on an array in mongodb?

I have an array with tags which is part of a document, eg
["red", "green", "blue", "white", "black"]
Now I want to find all documents, which have red AND blue.
Use the $all condition to find records that match both "red" and "blue" conditions.
db.my_collection.find({tags: { $all : ["red","blue"]}})
If you want records that match either "red" or "blue" then you can use the $in condition.
db.my_collection.find({tags: { $in : ["red","blue"]}})
Also in case someone is wondering how to negate itemized search, i.e. find record that match both "red" and "blue" and DO NOT match "green" and "white" this can be accomplished with the $nin operator, which may not be obvious when someone is reading $nin docs (http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24nin - parsing description was tricky for me):
db.my_collection.find({tags: { $all : ["red","blue"], $nin : ["green", "white" ]}})
This is very cool, as it allows relatively nice search syntax with negation:
tokenRequired1 tokenRequired2 !tokenForbidden1 !tokenForbidden2
Very natural, Gmail-style search.
As suggested here and here you could do full text search if you create an array of all tokens from a record, though I have no idea if it's efficient or even the best way to do it.