Install Mongodb on Ubuntu

Command line to install mongoDB on Ubuntu

sudo apt-get install gnupg
wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
sudo apt-get update
sudo apt-get install -y mongodb-org
sudo systemctl start mongod
sudo systemctl enable mongod
Add to my src(0)

No account yet? Register

Run Mongodb using docker command lines

Mongodb is easy to install, but it could also conflicts and stop working sometimes which is annoying, the best approach to solve this issue would be to always use it with docker like this:

mkdir ~/data
sudo docker run -d -p 27017:27017 -v ~/data:/data/db mongo

Add to my src(0)

No account yet? Register

Return value from Mongoose findOne sync call (parent-child dependency entity)

Rather than using methods chain and callback, from express router we can call to mongoose findOne using async await as following

we can then search for mongo, if found then use the object, not found then create new one and attach to the second entity.

// mongoose.model("Child", ChildSchema);
// mongoose.model("Parent", ParentSchema);

router.post('/create', async function (req, res, next) {
    const user = req.user;
    const childObject = req.body.object1;
    const parentObject = req.body.object2;
    console.log('create parent-child for user: ', user, ' from parent: ', parentObject, 'to child:', childObject);
    
    var parentEntity = await Parent.findOne({criteria1: parent.value1}).exec();
    if(!parentEntity) {
        parentEntity = new Parent(parentObject); 
        parentEntity.extra = 'extra';
        parentEntity.userId = user.id;      	
        parentEntity.save();
    }
  
    var childEntity = await Child.findOne({criteria1: child.value1}).exec();
    if(!childEntity) {
        childEntity = new Child(child);
        childEntity.extra = 'extra';
        childEntity.userId = user.id;
        childEntity.parentId = parentEntity.id;
        childEntity.save();
    }
});

This helps us solve the express mongoose mongodb object dependency issue

Add to my src(0)

No account yet? Register

Install MongoDB server on Fedora command

sudo vi /etc/yum.repos.d/mongodb.repo

[Mongodb]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/amazon/2/mongodb-org/4.2/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc

sudo dnf update
sudo dnf install mongodb-org
sudo systemctl enable mongod.service
sudo systemctl start mongod.service
mongod --version
mongo

> use test;
switched to db test
> db.test.save({'hello':'world'})
WriteResult({ "nInserted" : 1 })
> db.test.find()
{ "_id" : ObjectId("5f82b166af5d9be8c5bd0449"), "hello" : "world" }
> 
Add to my src(0)

No account yet? Register

Install Mongodb server on Debian / Ubuntu

sudo apt-get install gnupg
wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
echo "deb http://repo.mongodb.org/apt/debian buster/mongodb-org/4.4 main" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
sudo apt-get update
sudo apt-get install -y mongodb-org
sudo systemctl daemon-reload
sudo systemctl start mongod
sudo systemctl status mongod
sudo systemctl enable mongod

sudo systemctl stop mongod
sudo systemctl restart mongod
Add to my src(0)

No account yet? Register