How to Write query as like as mysql select and where in Mongodb - mongodb

How can I find data by multiple data as like as sql,Like I need to grab something Between date range where deliveryman===man,marchent===marchent,productStatus===status,Suppose I havent manId now so my query will return only data belongs to date range and matched with other conditions like marchent, productStatus, if i dont pass anything my query will return all data between selected date range. Date range will be available always, Thank you.
[{"pickup":false,"fee":88,"cost":0,"express":false,"returnReason":"","paid":false,"assigned":true,"_id":"5ff34e7f5dbbabb7b13828f4","orderid":"7dy3jl","description":"Laptop New Apple","receiverPhone":"01913198340","receiverName":"Masum","marchent":{"photo":"avatar.jpg","_id":"5fe9a35d1a92f45097499771","name":"Era","address":"Gulishtan","phone":"43535"},"deliveryman":{"photo":"avatar.jpg","_id":"5fe9a19f70d2bd4ee06fe9a0","name":"Era","phone":"345345345"},"weight":1,"expressTime":"","receiverAddress":"Nikunjo2,Road3,House7","productStatus":"Delivered","hub":{"photo":"avatar.jpg","_id":"5fe75679e6f7a62ddaf5b2e9","name":"Admin"},"createdAt":"2021-01-04T17:21:03.426Z","updatedAt":"2021-01-07T17:14:43.100Z","__v":0},{"pickup":false,"fee":88,"cost":0,"express":false,"returnReason":"","paid":false,"assigned":true,"_id":"5ff34b586eb76bad64501559","orderid":"xx1wfo89","description":"Smart TV","receiverPhone":"01913198340","receiverName":"adsf","marchent":{"photo":"avatar.jpg","_id":"5fe9a35d1a92f45097499771","name":"Era","address":"Gulishtan","phone":"43535"},"deliveryman":{"photo":"avatar.jpg","_id":"5fe9a14f70d2bd4ee06fe99f","name":"Raj","phone":"34354354"},"weight":1,"expressTime":"","receiverAddress":"asdf","productStatus":"in progress","hub":{"photo":"avatar.jpg","_id":"5fe75679e6f7a62ddaf5b2e9","name":"Admin"},"createdAt":"2021-01-04T17:07:36.351Z","updatedAt":"2021-01-07T18:44:27.695Z","__v":0},{"pickup":false,"fee":88,"cost":0,"express":false,"returnReason":"","paid":false,"assigned":true,"_id":"5ff34b307dca39ab609f5d23","orderid":"xx1wfo","description":"asdfasdf","receiverPhone":"01913198340","receiverName":"adsf","marchent":{"photo":"avatar.jpg","_id":"5fe9a35d1a92f45097499771","name":"Era","address":"Gulishtan","phone":"43535"},"deliveryman":{"photo":"avatar.jpg","_id":"5fe9a14f70d2bd4ee06fe99f","name":"Raj","phone":"34354354"},"weight":1,"expressTime":"","receiverAddress":"asdf","productStatus":"Cancelled","hub":{"photo":"avatar.jpg","_id":"5fe75679e6f7a62ddaf5b2e9","name":"Admin"},"createdAt":"2021-01-04T17:06:56.656Z","updatedAt":"2021-01-07T17:14:57.078Z","__v":0},{"pickup":false,"fee":88,"cost":0,"express":false,"returnReason":"","paid":false,"assigned":true,"_id":"5ff3453e41ef4d995d392002","orderid":"osa7mj","description":"Khata Colom","receiverPhone":"01913198340","receiverName":"Masum","marchent":{"photo":"avatar.jpg","_id":"5fe9a35d1a92f45097499771","name":"Era","address":"Gulishtan","phone":"43535"},"deliveryman":{"photo":"avatar.jpg","_id":"5fe9a14f70d2bd4ee06fe99f","name":"Raj","phone":"34354354"},"weight":1,"expressTime":"","receiverAddress":"Mohakhali dhaka","productStatus":"Delivered","hub":{"photo":"avatar.jpg","_id":"5fe75679e6f7a62ddaf5b2e9","name":"Admin"},"createdAt":"2021-01-04T16:41:34.041Z","updatedAt":"2021-01-07T15:11:27.359Z","__v":0},{"pickup":false,"fee":88,"cost":5000,"express":true,"returnReason":"","paid":false,"assigned":true,"_id":"5ff33dda15a2f69817cde3a8","orderid":"5iapq1","description":"Sauces","receiverPhone":"01913198340","receiverName":"Rezaul","marchent":{"photo":"avatar.jpg","_id":"5fe9a35d1a92f45097499771","name":"Era","address":"Gulishtan","phone":"43535"},"deliveryman":{"photo":"avatar.jpg","_id":"5fe9a1f71a92f4509749976f","name":"masum","phone":"34534543"},"weight":1,"expressTime":"tomorrow 10 pm","receiverAddress":"Nikunju-2,Road 3","productStatus":"on the way","hub":{"photo":"avatar.jpg","_id":"5fe75679e6f7a62ddaf5b2e9","name":"Admin"},"createdAt":"2021-01-04T16:10:02.029Z","updatedAt":"2021-01-07T17:01:49.712Z","__v":0},{"pickup":false,"fee":88,"cost":500,"express":false,"returnReason":"not reachable","paid":false,"assigned":true,"_id":"5ff33d7215a2f69817cde3a6","orderid":"252d6g","description":"Pickle","receiverPhone":"01913198340","receiverName":"Masum","marchent":{"photo":"avatar.jpg","_id":"5fe9a35d1a92f45097499771","name":"Era","address":"Gulishtan","phone":"43535"},"deliveryman":{"photo":"avatar.jpg","_id":"5fe9a1f71a92f4509749976f","name":"masum","phone":"34534543"},"weight":1,"expressTime":"","receiverAddress":"Nikunju2","productStatus":"return","hub":{"photo":"avatar.jpg","_id":"5fe75679e6f7a62ddaf5b2e9","name":"Admin"},"createdAt":"2021-01-04T16:08:18.481Z","updatedAt":"2021-01-07T17:15:16.026Z","__v":0},{"pickup":false,"fee":98,"cost":8000,"express":true,"returnReason":"","paid":false,"assigned":true,"_id":"5ff2f6c2652cf165286c1997","orderid":"iuc6l6","description":"Mango","receiverPhone":"01913198340","receiverName":"Raju","marchent":{"photo":"avatar.jpg","_id":"5fe9a3eb1a92f45097499773","name":"Lahore Shop","address":"Lahore, Dhaka","phone":"453453454"},"deliveryman":{"photo":"avatar.jpg","_id":"5fe9a14f70d2bd4ee06fe99f","name":"Raj","phone":"34354354"},"weight":3,"expressTime":"at 10pm","receiverAddress":"Mohammadpur, dhaka","productStatus":"in progress","hub":{"photo":"avatar.jpg","_id":"5fe75679e6f7a62ddaf5b2e9","name":"Admin"},"createdAt":"2021-01-04T11:06:42.123Z","updatedAt":"2021-01-04T11:06:42.123Z","__v":0},{"pickup":false,"fee":88,"cost":3000,"express":false,"returnReason":"","paid":false,"assigned":true,"_id":"5ff225ba78249040b81ada0e","orderid":"b2ij9u","description":"Phone Pack edited","receiverPhone":"01913198340","receiverName":"Masum ed","marchent":{"photo":"avatar.jpg","_id":"5fe98c05a843743e86a7849f","name":"Mishu Fashion","address":"asdfasdf","phone":"3453454"},"deliveryman":{"photo":"avatar.jpg","_id":"5fe9a1f71a92f4509749976f","name":"masum","phone":"34534543"},"weight":1,"expressTime":"","receiverAddress":"Nikunju-2","productStatus":"Cancelled","hub":{"photo":"avatar.jpg","_id":"5fe75679e6f7a62ddaf5b2e9","name":"Admin"},"createdAt":"2021-01-03T20:14:50.431Z","updatedAt":"2021-01-04T10:15:04.842Z","__v":0}]
So I need All value if I do not pass any extra filter keyword except Date Range, But If I send filter keyword like Product Status "Delivered" Only Delivered Product will be shown by the given date range, If I send Merchent id then all products belongs to this Merchent will be given, If I send Deliveryman Id then product status and deliveryman filter will be work. Final think my search query will be work depends on my filter query, my filter query might be null. If it is null then this filter should not be work but the rest of filter will work

Related

Filter results on the Firestore console by timestamp field

How can I filter the results on the Firestore Console by a timestamp field?
On the collection users, we have a field createdOn of type timestamp. If I want to filter the collection by field, I get the following dialog
I have tried entering the date as string
2019-09-15
2019-09-15T00:00:00Z
I have also tried using a timestamp as number in millis and seconds
1568505600000
1568505600
When looking at the requests sent to Firestore, the structured query uses a field filter with a value of corresponding to either stringValue or integerValue, but I think the timestampValue would be the right thing.
I do not want to adapt the query in the console and build my own requests. I know that there is always the option to sort the documents in the collection and then scroll to where it's interesting, but this will load all documents that are not relevant.
Is there a simple way to do what I want?
There is a new query builder tab in the console (I do not know when this was introduced, but I assume during the Firebase Summit 2022). For the query above, this would look like this
It even has a timestamp type in the select list.

Find documents with a certain field value only when another field value is a given string

I'm using this php package to make queries - https://github.com/jenssegers/laravel-mongodb
The situation is, there are two fields, user_id and post_status among others. I want to retrieve all the documents in that collection, but when post_status field value is draft, that should be retrieved only when user_id is a given string. The idea is, only logged in user finds their drafted posts among other posts.
I'm having hard time finding any solution for this problem. The app is still not in production. If I should store data is some different manner, that is an option as well.
Find documents with a certain field value only when another field value is a given string
The question your are framing is simply convert into a and query, how let's see it
when another field value is a given string
This means that you have some result sets and you need to filter out when user_id match with some string. i.e some result sets and user_id = <id>
Now consider the first part of the sentence Find documents with a certain field value
This means you are filtering the records with some values i.e "status" = "draft" and whatever result will come and want again to filter on the basis of user_id = <id>
So finally you will end-up with below query:
db.collectionName.find({"status":"draft", "user_id": "5c618615903aaa496d129d90"})
Hope this explanation will help you out or you can rephrase your question I will try to modify by ans.

Firestore order by time but sort by ID

I have been trying to figure out a way to query a list of documents where I have a range filter on one field and order by another field which of course isn't possible, see my other question: Order by timestamp with range filter on different field Swift Firestore
But is it possible to save documents with the timestamp as id and then it would sort by default? Or maybe hardcode an ID, then retrieve the last created document id and increase id by one for the next post to be uploaded?
This shows how the documents is ordered in the collection
Any ideas how to store documents so they are ordered by created at in the collection?
It will order by document ID (ascending) by default in Swift.
You can use .order(by: '__id__') but the better/documented way is with FieldPath documentID() I don't really know Swift but I assume that it's something like...
.order(by: FirebaseFirestore.FieldPath.documentID())
JavaScript too has an internal variable which simply returns __id__.
.orderBy(firebase.firestore.FieldPath.documentId())
Interestingly enough __name__ also works, but that sorts the whole path, including the collection name (and also the id of course).
If I correctly understood your need, by doing the following you should get the correct order:
For each document, add a specific field of type number, called for example sortNbr and assign as value a timestamp you calculate (e.g. the epoch time, see Get Unix Epoch Time in Swift)
Then build a query sorted on this field value, like:
let docRef = db.collection("xxxx")
docRef.order(by: "sortNbr")
See the doc here: https://firebase.google.com/docs/firestore/query-data/order-limit-data
Yes, you can do this.
By default, a query retrieves all documents that satisfy the query in
ascending order by document ID.
See the docs here: https://firebase.google.com/docs/firestore/query-data/order-limit-data
So if you find a way to use a timestamp or other primary key value where the ascending lexicographical ordering is what you want, you can filter by any fields and still have the results sorted by the primary key, ascending.
Be careful to zero-pad your numbers to the maximum precision if using a numeric key like seconds since epoch or an integer sequence. 10 is lexicographical less than 2, but 10 is greater than 02.
Using ISO formatted YYYY-mm-ddTHH:MM:SS date-time strings would work, because they sort naturally in ascending order.
The order of the documents shown in the Firebase console is mostly irrelevant to the functioning of your code that uses Firestore. The console is just for browsing data, and that sorting scheme makes it relatively intuitive to find a document you might be looking for, if you know its ID. You can't change this sort order in the console.
Your code is obviously going to have other requirements, and those requirements should be coded into your queries, without regarding any sort order you see in the dashboard. If you want time-based ordering of your documents, you'll have to store some sort of timestamp field in the document, and use that for ordering. I don't recommend using the timestamp as the ID of a document, as that could cause problems for you in the future.

Solr: Query for documents whose from-to date range contains the user input

I would like to store and query documents that contain a from-to date range, where the range represents an interval when the document has been valid.
Typical use cases in lucene/solr documentation address the opposite problem: Querying for documents that contain a single timestamp and this timestamp is contained in a date range provided as query parameter. (createdate:[1976-03-06T23:59:59.999Z TO *])
I want to use the edismax parser.
I have found the ms() function, which seems to me to be designed for boosting score only, not to eliminate non-matching results entirely.
I have found the article Spatial Search Tricks for People Who Don't Have Spatial Data, where the problem described by me is said to be Easy... (Find People Alive On May 25, 1977).
Is there any simpler way to express something like
date_from_query:[valid_from_field TO valid_to_field] than using the spacial approach?
The most direct approach is to create the bounds yourself:
valid_from_field:[* TO date_from_query] AND valid_to_field:[date_from_query TO *]
.. which would give you documents where the valid_from_field is earlier than the date you're querying, and the valid_to_field is later than the date you're querying, in effect, extracting the interval contained between valid_from_field and valid_to_field. This assumes that neither field is multi valued.
I'd probably add it as a filter query, since you don't need any scoring from it, and you probably want to allow other search queries at the same time.

Is there a way to fetch max and min values in Sphinx?

Im using sphinx for document search. Each document has list of integer parameters, like "length", "publication date (unix)", popularity, ... .
The search process itself works fine. But is there a way to get a maximum and minimum fields values for a specified search query?
The main purpose is to generate a search form which will contain filter fields so user can select document`s length.
Or maybe there is another way to solve this problem?
It is possible if length, date etc are defined as attributes.
http://www.sphinxsearch.com/docs/current.html#attributes
Attributes are additional values
associated with each document that can
be used to perform additional
filtering and sorting during search.
Try GroupBy function by 'length' and select mix(length), max(lenght).
In SphinxQl it is like:
select mix(length), max(lenght) from index_123 group by length
The same for other attributes.