CouchDB longpoll without _changes - couchdb

I want to use the longpoll option in couchDB, but without the _changes view.
The database has this view:
function (doc) {
if(doc.job)
emit(doc._id, 1);
}
So it emits all documents that have a tag “job” in them. Note that I have a bunch of other stuff in this DB. Now I want to use a longpoll where the connection stays open as long as there are no documents in the DB with the job tag. As soon as there is a document with the job tag, the db send it to the client and the connection should be closed. The client can now do the job, send a delete command to the db to delete the job, and start listening again.
So my idea is to call the view somehow like this:
http://mycouch/mydb/_design/visualize/_view/get_jobs?feed=longpoll&include_docs=true
However, it seems it is only possible to use longpoll with the _changes view. Any workaround?
Thanks for any help

_changes is the feed you need to use in your case. Yo can use it filtering the feed by receiving changes only for "job" tagged documents. You can do this using your view map function.
http://mycouch/_changes?filter=_view&view=visualize/get_jobs&feed=logpoll&include_docs=true
You'll receive through this feed any update (create/update/delete) over "job" tagged docs in your database.

Related

How to update data fields on the server-side?

How I write a script and update a collection field from my server?
I need to update a field of 100 users.
I was wondering if I can change them from the server, instead of sending update mutations over and over?
The Queries part of Strapi Documents says:
You can just call strapi.query('modelName', 'pluginName') to access
the query API for any model.
But it's not clear where to run it?
Do I need to create a plugin? or Can I create an updateUserData.js file and run node updateUserData.js?
strapi.query('user').update( { id: 1 },...

Monitor updates on a MongoDB collection (Post update hook on db layer)

One of my collection is getting updated by different micro-services. And the updates are being done by either monk or mongoose libs.
So I wanted to monitor updates done on this collection. Also I do not have access to those services so I cannot add post update hooks on each services but I have access to db where I can do whatever I want. I am open to use any ORMs or libs.
So basically - if any update query is triggered from any of the
services, my monitoring (hook or something) code should be triggered.
Thanks.
You can use mongodb Change Streams to listen for data changes check out a sample app https://gist.github.com/riodw/74a839ab6964bceda8ff799d3ad33442
Mongodb docs https://docs.mongodb.com/v3.6/changeStreams/#change-streams
You can try the change stream from mongoose :
https://mongoosejs.com/docs/models.html#change-streams
An example i just tried and it works :
let Congress = require("../models/congress");
// change stream
Congress.watch().
on('change', data => console.log('change stream',data));
//

Azure Cosmos DB SQL API UPDATE statement - don't replace whole document

Can I write an UPDATE statement for Azure Cosmos DB? The SQL API supports queries, but what about updates?
In particular, I am looking to update documents without having to retrieve the whole document. I have the ID for the document and I know the exact path I want to update within the document. For example, say that my document is
{
"id": "9e576f8b-146f-4e7f-a68f-14ef816e0e50",
"name": "Fido",
"weight": 35,
"breed": "pomeranian",
"diet": {
"scoops": 3,
"timesPerDay": 2
}
}
and I want to update diet.timesPerDay to 1 where the ID is "9e576f8b-146f-4e7f-a68f-14ef816e0e50". Can I do that using the Azure SQL API without completely replacing the document?
The Cosmos DB SQL language only supports the Select statement.
Partially updating a document isn't supported via the sql language or the SQL API.
People have made it clear that that's a feature they want so there is a highly upvoted request for it that can be found here: https://feedback.azure.com/forums/263030-azure-cosmos-db/suggestions/6693091-be-able-to-do-partial-updates-on-document
Microsoft has already started to work on that so the only thing you can do is wait.
To elaborate more on this, Updating partially a document in CosmosDb on the server isn’t possible, instead you can do whatever you need to do in the memory.In order to literally UPDATE the document, you will have to to retrieve the entire document from the CosmosDb, update the property/properties that you need to update and then call the ‘Replace’ method in the CosmosDb SDK to replace the document in question. Alternatively you can also use ‘Upsert’ which checks if the document already exists and performs an ‘Insert’ if true or ‘Replace’ if false.
NOTE : Make sure you have the latest version of the document before you commit an update!
UPDATE :
CosmosDB support for Partial Update is GAed. You can read more from here
You can use Patch API for partial updates.
Refer: https://learn.microsoft.com/en-us/azure/cosmos-db/partial-document-update
As of 25.05.2021 this feature is available in private preview and will hopefully be in GA soon.
https://azure.microsoft.com/en-us/updates/partial-document-update-for-azure-cosmos-db-in-private-preview/
Use Robo3T to update/delete multiple records with mongo db commands. Make sure your query includes the partition key.
db.getCollection('ListOfValues').updateMany({type:'PROPOSAL', key:"CITI"},{$set: {"key":"CITIBANK"}})
db.getCollection('ListOfValues').deleteMany({type:'PROPOSAL', key:"CITI"})

MeteorJS subscription mechanism

I have a question about subscription/publication in meteor.
When you subscribe to a publication in meteor, is the client database updated or only a copy at one moment of the server database?
In fact, will Meteor update the local database after a subscription, or will it just be an image of the server's database at a T moment where T is the moment of subscription ? And can I get the last data when I call Collection.find() on client side ?
The quick answer is that as long as your subscription is still active, Meteor will keep the client in sync with the server per the rule you defined in the publish() method. It is not a copy at time T, it starts at time T and is amended as the server collection changes.
For example:
/server/publish/people.js:
Meteor.publish('people', function() {
return People.find();
}
/client/app.js:
Meteor.subscribe('people');
The publish()'s 'People.find()' will be monitored and any changes to the query will be replicated on the client. If you have reactive queries (People.find() in a template helper for example) on the client, those will be re-executed automatically and the template updated (see Tracker).
Good reference for you: https://www.discovermeteor.com/blog/understanding-meteor-publications-and-subscriptions/

User specific database in MongoDB

I am currently working on an inventory management software in Node js and MongoDB. I am pretty new to MongoDB, having worked in Oracle and MySQL for most of my projects.
Is it possible to create a separate database schema for every client who uses my software, with each client having access only to his copy of the database schema and collections?
The equivalent of selecting data in Oracle database would be
Select * from User1.table,
Select * from User2.table etc
Also, if it were possible, how would it be implemented using a node js mongo db client like mongoose?
I looked at MongoDB documentation, but it talks mainly about adding users to a database for authorization.
I apologize if it seems like a silly question, but id appreciate it if someone could point me in the right direction for this.
Before starting to invest a lot of time in the development of your project, check out other possible approaches to the scenario that you are trying to build.
I did a quick search on SO and found some additional threads with similar scenarios:
MongoDB Database vs. Collection
MongoDB Web App - Database per User
Additional info about mongoose database creation
Whenever you call the connect method on the mongoose object, you are either connecting to an existing database or you are creating it in case it doesn't already exist.
You could have a function that allows you to pass in a name argument with the name and create databases programmatically:
function createDatabase(name) {
var conn_string = 'mongodb://localhost/';
if (typeof name == 'string') {
conn_string += name;
}else{
return false;
}
mongoose.connect(conn_string);
}
Also, be aware that a database will be created when you first insert a record in a collection of that particular database.
It is not sufficient to only connect to the database, you also have to insert a record.
As per my previous example, you could also pass a schema parameter to the function, tailored to each user's profile and fire an insert statement after you connect to that database.

Resources