Lots of solutions to querying mongoDB using date/time field in MongoDB but what if the mongo doc doesn't have a date/time field?
I've noticed that when I hover the mouse over a document _id (using NoSQLBooster for MongoDB) I get a "createdAt" dropdown (see screenshot below). Just wondering if there is anyway to do a query using pymongo where documents are filtered based on a date/time range using their "createdAt" metadata?
In MongoDB the id of the docs contains the timestamp of creation, this is mentioned on this other question.
You can make a script that insert a date/field using this information to perform those queries or perform the query directly to using the objectId as in here.
Related
in my mongodb, i have a collection where the docs are created not using ObjectId, how can I get the timestamp (generation_time in pymongo) of those docs? Thank you
If you don't store timestamps in documents, they wouldn't have any timestamps to retrieve.
If you store timestamps in some other way than via ObjectId, you would retrieve them based on how they are stored.
i am using python with firebase SDK, and have a table named jobs, each record, has a field named client, that is a map, each client has an id field. I would like to query the table for all the jobs that have the client with a certain id value, I found this explaining for how to query by array members but can't find anything about query by values of map fields.
will something like
.where("client.id", "==", id) work and be effective? how can I do this query in an effective way? create index maybe?
enter code here
It should work without creating an index. What you wrote should filter on the id property of the client field object for all documents of a collection.
See also:
Firestore: Query documents by property of object
Query Google Firestore database by custom object fields
Is that possible to filter document id based on date? For example, documents are inserted daily and we want to delete the previous date data. We planned to append date along document id, and perform deletion with filtering on document ID with wildcard - 20181101_* to delete all the document which their id start with some matching date.
Another approach will be inserting a date field in each document to run a WHERE clause.
q = doc_ref.where(u'date', u'==', 20181101).get()
I got this, but just wondering if there is a better approach.
perform deletion with filtering on document ID with wildcard - 20181101_*
There is no way in Cloud Firestore to create a query based on wildcards.
delete all the document which their id start with some matching date.
There is also no way in which you can query elements in a Firestore database that start with some matching date. To solve this, you should follow the instructions from the official documentation which says:
Cloud Firestore doesn't support native indexing or search for text fields in documents. Additionally, downloading an entire collection to search for fields client-side isn't practical.
To enable full text search of your Cloud Firestore data, use a third-party search service like Algolia.
And yes, you are guessing right, the best solution is to to add a new date property and to query the database according to it.
Folks,
Retrieving all items from a DynamoDB table, I would like to replace the scan operation with a query.
Currently I am pulling in all the table's data via the following (python):
drivertable = Table(url['dbname'])
all_drivers = []
all_drivers_query = drivertable.scan()
for x in all_drivers_query:
all_drivers.append(x['number'])
How would i change this to use the query API?
Thanks!
There is no way to query and get the entire results of the table. As of right now, you have a few options if you want to get all of your data out of a DynamoDB, and all of them involve actually reading the data out of DynamoDB:
Scan the table. It can be done faster with the expense of using much more read capacity by using a parallel scan
Export your data using AWS Data Pipelines. You can configure the export job for where and how it should store your data.
Using one of the AWS event platforms for new data and denormalize it. For all new data you can get a time-ordered stream of all updates to the table from DynamoDB Update Streams or process events using AWS Lambda
You can't query an entire table. Query is used to retrieve a set of items by supplying a hash key (part of the complex primary key hash-range of the table).
One can not use query without knowing the hash keys.
EDIT as a bounty was added to this old question that asks:
How do I get a list of hashes from DynamoDB?
Well - In Dec 2014 you still can't ask via a single API for all hash keys of a table.
Even if you go and put a GSI you still can't get a DISTINCT hash count.
The way I would solve this is with de-normalization. Keep another table with no range key and put every hash there together with the main table. This adds house-keeping overhead to your application level (mainly when removing), but solves the problem you asked.
I'm using Flask-MongoEngine in my python application, and I'm trying to grab a list of documents WHERE a field equals some value. I know how to grab a single document based on the value of a field using get(name="chris"), but how would I be able to do this with returning multiple documents? Nothing in the docs is really sticking out.
MongoEngine Document classes have an objects attribute, which is used for accessing the objects in the database associated with the class. example:
uk_users = User.objects(country='uk')
For advanced queries you can use the filter attribute:
uk_female_users = User.objects(country='uk').filter(gender='f')
This is the related documentation MongoEngine - Querying the database