get all docs from couchDB updated in a specific time range - couchdb

I would like to get all the docs in couchDb updated in a specific time range.
I'm using the below API but I don't get any result.
/_all_docs?startkey="2019-01-01T00:00:00Z"&endkey="2020-01-01T00:00:00Z"
Any suggestions are welcome.
Andrea

_all_docs's key is the document ID, not timestamp. For your query to be useful, you'll need to create a custom view based on a timestamp (and ensure the timestamp is updated by your code).

Related

StorIO observeChangesInTable, can i get updated rows/items with it?

StorIOSQLite has interesting method observeChangesInTable(). And When I saw it I thought that it would observe changes in given table and return list of updated items.
But it just returns updated table name. Why would I need an updated table name? I can just subscribe to hole table to be notified about an update
storIoSqLite.get()
.listOfObjects(Item.class)
.withQuery(
Query.builder()
.table(ItemTable.NAME)
.build()
)
.prepare()
.asRxObservable()
Please explain what is the point of observeChangesInTable() method. And what is the best solution for my problem.
With your approach you're actually doing a query to the db and only then reacting to the change.
With StorIOSQLite.observeChangesInTable() you can react on changes in the table without doing any queries to the db. This is much much cheaper and should be used in situations when you need to do a debounce() or window(), filter() etc and only then make actual query to the db.
Hope that helps!

ObjectID with conventions

I need to generate ids with a convention, for example:
Instead of getting: "538cd180e381f20d1c1cd2a2"
I would like to have an ID like this one: "p38cd180e381f20d1c1cd2a2"
So what I want is that my IDs start with a consonant letter.
Does anyone know how to accomplish that within the driver, I mean, getting that behaviour on "new mongo.ObjectId()"?
Thanks in advance.
You can use the following, to get the id starting with a consonant
db.collection.insert({"_id":"p"+new ObjectId()})
you can use any other string in place of "p" and the string will append to the start of the id generated by mongodb.
Short answer: Sorry, no standard way available to achieve this as of now.
Detailed answer and workaround: MongoDB or driver generated ids are a combination of Creation Time (as timestamp), Increment value for next id, Machine on which the id is generated and the process id of the process which generated this document id. All this info is available in the generated id and can be extracted back. For now, this is what you have been given and there is no support for generating your own custom id from the driver's algorithm.
If you want to customize your id generation and be able to make use of these properties, then you can embed all this info that MongoDB uses for id generation and add this information to your document itself. By doing that you will be able to reproduce the information that MongoDB generates from the id. And while inserting the document to MongoDB, you can give your docs a customized id which agrees with your requirements.
So if you later on want to make comparisons based on creation time or maybe the machine, you can do that from the information that was added to the docs themselves.
Use the code: db.collection.insert({"customId":"p"+new ObjectId()}). And let your code use this customId.

what's the best way to bind a mongodb doc to a node.js html page

In past with my PHP / Rails - MYSQL apps I've used the unique ID of a table record to keep track of a record in an html file.
So I'd keep track of how to delete a record shown like this (15 being the ID of the record):
Delete this record
So now I'm using MongoDB. I've tried the same method but the objectID ._id attribute seems to be a loooong byte string that I can't use conveniently.
What's the most sensible way of binding a link in the view to a record (for deletion, or other purposes or whatever)?
If the answer is to create a new id that's unique for each document in the collection, then what's the best way to generate those unique id's?
Thank you.
You could use a counter instead of the ObjectID
But this could create a problem when inserting a new document after you deleted a previous one.
See this blog post for more detail info on Sequential unique identifiers with Node.js and MongoDB.
Or you could use the timestamp part of the ObjectID:
objectId.getTimestamp().toString()
See the node objectid docs

How to get last created document in couchdb?

How can I get last created document in couchdb? Maybe some how I can use _changes feature of couchdb? But documentation says, that I only can get list of document, ordered by first created document, ant there is no way to change order.
So how can I get last created document?
You can get the changes feed in descending order as it's also a view.
GET /dbname/_changes?descending=true
You can use limit= as well, so;
GET /dbname/_changes?descending=true&limit=1
will give the latest update.
Your only surefire way to get the last created document is to include a timestamp (created_at or something) with your document. From there, you just need a simple view to output all the docs by their creation date.
I was going to suggest using the last_seq information from the database, but the sequence number changes with every single write, and replication also complicates the matter further.

How to get Post with Comments Count in single query with CouchDB?

How to get Post with Comments Count in single query with CouchDB?
I can use map-reduce to build standalone view [{key: post_id, value: comments_count}] but then I had to hit DB twice - one query to get the post, another to get comments_count.
There's also another way (Rails does this) - count comments manually, on the application server and save it in comment_count attribute of the post. But then we need to update the whole post document every time a new comment added or deleted.
It seems to me that CouchDB is not tuned for such a way, unlike RDBMS when we can update only the comment_count attribute in CouchDB we are forced to update the whole post document.
Maybe there's another way to do it?
Thanks.
The view's return json includes the document count as 'total_rows', so you don't need to compute anything yourself, just emit all the documents you want counted.
{"total_rows":3,"offset":0,"rows":[
{"id":...,"key":...,value:doc1},
{"id":...,"key":...,value:doc2},
{"id":...,"key":...,value:doc3}]
}

Resources