Say I have a document, which looks like
{loc:{start:[x,y],end:[x1,y1]}}
with 2dsphere index, and I want to search for all the documents that have the same start and end points in one query. Is it possible to do it in MongoDB?
A geospatial index can only cover one field with a legacy coordinate pair (array with two values) or a valid GeoJSON Point, Polygon or LineString. That means you can only index either the start-position or the end-position with a geospatial index.
But this is not a problem when you are searching for an exact match. A 2dsphere index offers some additional geographical operators, but when you only need to test for exact equality, you don't need those and can treat your geo-coordinates like plain old data. So when you just add a vanilla index on the loc field and use a vanilla find() with the values you are searching, you will get your matches.
Related
I want to query a database for the first 100 documents that are closest to me in MongoDB. Once I have the closest 100 documents, I want to sort them by custom fields in the documents. Such as createdAt or points. It seems like $near is what I want, but their docs say:
When using sort() with geospatial queries, consider using $geoWithin operator, which does not sort documents, instead of $near.
So it seems like they suggest using $geoWithin, but I don't want to constrain the search to a specific range. Suggestions?
I have the following documents:
{'variations': ['BlueViolet', 'CadetBlue', 'Cyan']}
{'variations': ['LightPink', 'VioletRed']}
And I want to write a query that selects all documents where size of intersection between variations field and {'Cyan', 'CadetBlue', 'SmoothsRed'} is greater than 2.
Can this be performed with mongodb operators?
This link explains how to achieve what you want - https://docs.mongodb.com/manual/reference/operator/aggregation/setIntersection/
In order to write the comparison, I'll have to assume each document can be uniquely identified using an _id value, and then you can write your query using the solution given in this answer - How to find set intersection of sets between the documents in a single collection in MongoDB?
Good luck
I have a collection of geospatial+temporal data with a few additional properties, which I'll be displaying on a map. The collection has a few million documents at this point, and will grow over time.
Each document has the following fields:
Location: [geojson object]
Date: [Date object]
ZoomLevel: [int32]
EntryType: [ObjectID]
I need to be able to rapidly query this collection by any combination of location (generally a geowithin query), Date (generally $gte/$lt), ZoomLevel and EntryType.
What I'm wondering is: Should I make a compound index containing all four fields, or a single index for each field, or some combination thereof? I read in the MongoDB docs the following:
For a compound index that includes a 2dsphere index key along with
keys of other types, only the 2dsphere index field determines whether
the index references a document.
...Which sounds like it means having the 2dsphere index for Location be part of a compound index might be pointless?
Any clarity on this would be much appreciated.
For your use case you will need to use multiple indexes.
If you create one index covering all fields of your documents your queries will only be able to use it when they include the first field in the index.
Since you need to query by any combination of these four fields I suggest you to analyze your data access patterns and see exactly what filters are you actually using and create specific index for each one or group of them.
EDIT: For your question about 2dsphere, it does make sense to make them compound.
This note refers to the 'sparse' option. Sparse index references only documents that contains the index fields, for 2dspheres the only documents that will be left out is the ones that do not contain the geojson/point array.
I am new to mongodb and want to make indexes for a specific collection. I have seen people use a digit "1" in front of the field name when they want to create an index. for example:
db.users.ensureIndex({user_name: 1})
now I want to know what does this digit mean and is it necessary to use it?
It's the type of index. MongoDB supports different kinds of indexes. However, only the first two indexes can be combined to a compound index.
1: Ascending binary-tree index.
-1: Descending binary-tree index. Very similar to the default index but the difference can matter for the behavior of compound indexes.
"hashed": A hashtable index. Very fast for lookup by exact value, especially in very large collections. But not usable for inexact queries ($gt, $regex or similar).
"text": A text index designed for searching for words in strings with natural language.
"2d": A geospatial index on a flat plane
"2dsphere": A geospatial index on a sphere
For more information, see the documentation of index types.
It defines the index type on that specefic field. For example the value of 1 creates an index with ascending order, while the value -1 create the index with descending order.
For more information, see the Manual
Hibernate Search allows to sort search results on relevance. Is it possible to obtain and display (e.g. in a jsp view) this information using Lucene query?
A Query in Hibernate Search can return Projections rather than the simple list of matching entities.
A projection result essentially means each result is an array containing the sequence of projections you asked for. Normally this is used to extract text from a specific field, so to not need loading the data from the database, but there are Projection constants to return also the Score value or the Explanation of the scoring.
query.setProjection( ProjectionConstants.SCORE, ProjectionConstants.EXPLANATION, ProjectionConstants.THIS );
See also the Reference documentation on projections explaining this and more.