Updating pre-existing things already in a database mongodb - node.js

I'm currently using mongodb in a express application via mongoose and a random thought came upon me. What would happen if say you had a site with users and later on you made a update and needed to add a new field to all the user models. how would you go about updating all pre-existing users with the new field.

You would use updateMany in your mongo shell (or using driver - the same):
db.user.updateMany({}, { $set: { newField: value } });

Related

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));
//

Mongoose - Keep local database in sync with remote database

I have access to two separate databases that I'd like to keep in sync, a new one and an existing one, which will be in separate physical locations. The new one is going to be used to service an external API, so to cut down on request time, I think it makes sense to only query the local database for API requests.
My initial approach was to use mongoose.createConnection and limit the local collection to minor metadata and directly access the remote collection, but that's what I'm now looking to avoid.
Another approach might be to use mongoose.createConnection to periodically query the remote db and update the local one, but it could be costly if I want to do make frequent updates.
There are ways to cut down the cost - for example, there is a lastUpdated property in the relevant collection on the existing database, which could be used to limit the remote query to recently updated records such as:
RemoteCollection.find({
lastUpdated: {$gte: Date.now() - lookbackPeriod}
})
But I'm wondering if there's any native functionality of mongoose/mongoDB that can be used more efficiently make the updates. I also thought about mongodump and mongorestore to keep a full local copy of the records I needed, but that also seems costly.
Any help is appreciated.
After a bit of reading and thanks to Jake's comment, it looks like it's working. I need to do some more setup, but the code below should work and is based on this section from the docs:
https://mongoosejs.com/docs/models.html#change-streams
The first step would be start mongod with the --replSet flag:
mongod --replSet "rs0" --bind_ip localhost,<hostname(s)|ip address(es)>
Then close and restart mongo and run rs.initiate() on the database. You can then check the status of the replica set with rs.status(). If that command works and returns a result, the replica set functionality should be there.
Then within Node, you can do something like this:
// The docs reference creating a new model but you can just import an existing one
const RemotePerson = require('./models/RemotePerson');
const LocalPerson = require('./models/LocalPerson');
RemotePerson.watch().on('change', data => {
if (data.operationType === "insert") {
LocalPerson.create(data.fullDocument);
} else if (data.operationType === "update") {
LocalPerson.findByIdAndUpdate(data.documentKey, {
$set: data.updateDescription.updatedFields
});
}
});

How to delete a database using node.js and MongoDB Atlas?

I'm new to MongoDB and node.js and I want to know how to delete a database.
I've searched for and I found that it is done with the dropDatabase() method. The problem is that it shows a MongoError: user is not allowed to do action [dropDatabase] on [test.]. I also searched for this error and the answer I found is that the user needs to be registered as Data Access Read Write. However, my user in mongo atlas is registred as Project Owner and as the answer says. I removed Project Owner and it still displays the error.
MongoClient.connect(uri, { useNewUrlParser: true }, (err, client)=>{
if(err) throw err;
console.log("Connected...");
const db = client.db("test");
db.dropDatabase();
console.log("asdfghj")
client.close();
});
In fact, whenever i searched to fix the problem typing Mongo atlas in the search bar, the results are like How to delete cluster in mongo atlas even though i'm typing delete database in mongo atlas. So i don't know if a database can be deleted in mongo atlas.
Thank you in advance.
Looks like you are confusing "Atlas Project" security with "Database Access".
If you are looking for removing a database from an Atlas Cluster, the "Database User" you are using to login in your Mongo Client needs to have "dbAdmin" role in the related database. You can then perform related operations using your database user via your Mongo client programmatically.
You can refer to https://docs.atlas.mongodb.com/security-add-mongodb-users/ for managing database users and security roles.
You can log into Atlas, click the cluster name, click the "collections" tab, then when you hover over the database name on the left, there will appear a garbage can icon, which will delete the DB (assuming your user has proper permissions)
If you want to delete the db in code, there's no way to do it directly from mongoose, but you can if you access the underlying mongodb driver:
mongoose.connection.db.dropDatabase(cb)

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.

From where can we decide collection name in use of "sails-mongoose" package, in node.js + sailsjs?

I had code for Connection with mongo with username, password, host, DBName etc in adapter.js,
schema in model,
and for CRUD we can code in controller. but from where can we select particular collection of selected DB of mongo?
I am the creator of Sails-mongoose. I've not really continued my work with sails-mongoose because to use mongoose, you will have to cut out Waterline which is built completely into the eco system of Sailsjs.
I would recommend using against the library i myself built.
Apologies & Thanks
It will automatically create or use model's name collection in selected DB of mongo.

Resources