How to edit a particular record inside a Couchdb's document - nosql

We have a document which has 50k records.
We are trying to edit a particular record from the document but when we do it the whole documents gets overwritten.
How can we edit a record from the document without losing all the data from the same document?

Related

Firestore array-not-contains alternative solution

TL-DR
I have created a Flutter Firestore posts application. I want to present the user only new posts, which they didn't read yet.
How do I achieve that using Firestore query?
The problem
Each time a user sees a post, their id is added to the post views field.
Next time the user opens the app, I want to present only posts they didn't read yet.
The problem is that query array-not-contains is not supported. How do I achive that functionality?
You're going to have a real hard time with this because Firestore can only give you documents where you know something about the contents of that document. That's how indexes work - by recording data present in the document. Indexes don't track data not present in a document because that's basically an infinite amount of data.
If you are trying to track documents seen by the user, you would think to mark the document as "seen" using a boolean per user, or tracking the document ID somewhere. But as you can see, you can only query for documents that the user has seen, because that's the data present in the system.
What you can do is query for all documents, then query for all the documents the user has seen, then subtract the seen documents from all documents in order to get the unseen documents. But this probably doesn't scale in a way you'd like. (It's essentially the same problem with Firestore indexes not being able to surface documents without some known data present. Firestore won't do the equivalent of a SQL table scan, since that would be a lot of reads you'd have to pay for.)
You can kind of fake it by making sure there is a creation timestamp in each document, and record for each user the timestamp of the most recent seen document. If you require that the user must view the documents in chronological order, then you can simply query for documents with a creation timestamp greater than the timestamp of the latest document seen by the user. This is really as good as it's going to get with Firestore, since you can't query for the absence of data.

Check the subcollection exists before query in firestore

I'm implementing a social media app, where I put a subcollection of "following" under each user. I want to check if the subcollection exists before I query the subcollection, or the app will crash for querying a nonexistent collection. Is there a way to check this?
Collections don't really "exist" in the way that you're thinking. They simply appear when the first document is created, and they disappear when the last document is removed. There is no operation to simply create or remove a collection like a folder in a filesystem, and there is not operation to check to see if a collection "exists". A query against a collection with no documents will not fail (unless it was rejected by a security rule).
The only thing you can really do is query the collection to see if it has any documents at all. You can limit the query to 1 document if you want to minimize costs.

Adding document and subcollection to a collection - firestore python

We have a collection in Google Firestore.
Can we add a document with data and a subcollection under this new document in one statement?
In one statement, no. Strictly speaking, most programming languages are going to require at least one statement for each document to write using the provided APIs.
In one batch, yes. You can simply do a batch write to create any two documents simultaneously. Parent documents and subcollections do not need to exist in order to create a nested document, so there is not even any requirement to create the parent document, if that's what you were wondering.
Also, you can't create a subcollection that doesn't "exist". It will exist when the first document is written, and disappear when the last document is deleted.

How to get inserted or updated documents only in MongoDB

I am using MongoDB in my web API. MongoDB is being updated/inserted by other sources.
How do I query mongodb to get only newly inserted or updated documents ?
I can get sorted documents by below query but this doesn't solve my purpose
db.collectionName.findOne({}, {sort:{$natural:-1}})
Is there any log or other way like in SQL there is INSERTED and UPDATED
What are these newly inserted/updated documents in your context?
Assuming that you need to fetch newly/inserted documents relative to a time, you need to have a field in your collection that holds the time stamp (for example, inserted, and lastUpdated fields), you have an operator to help with updating. But this needs application changes.
Or you can use change streams, for a trigger like functionality. You can listen for changes and take actions as changes are made.

How to store the details about modified documents in another collection in Mongodb?

I need to store the details of the modified fields in a collection and store those details in the another collection. For example if i update the field "Status" the new value and old value should be stored in another collection along with the _id of the modified documents. Is it possible to do it? I know that we cannot use Trigger in MongoDB. I tried to used log files for it. But not able to get the old value of updated document using it. Please let me know the possible way. I'm very much stuck here.