viernes, 29 de septiembre de 2017

Mongodb: Update

Documentation:
https://docs.mongodb.com/manual/reference/operator/update/

Definition

db.collection('db_name').findOneAndUpdate({
      // Query
    }, {
      // Update
    }, {
      // Options
    }).then((result) => {
  });

Example:

db.collection('Todos').findOneAndUpdate({
      _id: new ObjectID("59ce69b18bec45c336c4ef4a")
    }, {
      $set: {
        completed: true
      }
    }, {
      returnOriginal: false
    }).then((result) => {
  });

Mongodb: Delete

Setup code

// const MongoClient = require('mongodb').MongoClient;
const {MongoClient, ObjectID} = require('mongodb');

MongoClient.connect('mongodb://localhost:27017/your_app', (err, db) => {
  if (err) {
    return console.log('Unable to connect to MongoDB server');
  }
  console.log('Connected to MongoDB server');

  // Execute code here
});

Delete many

Deletes all documents that match the criteria, DOESN'T return any documents.

  db.collection('your_db').deleteMany({query_object}).then((result) => {
    console.log(result);
  });

Delete one

Deletes the first document that matches the criteria, DOESN'T return that document.

  db.collection('your_db').deleteOne({query_object}).then((result) => {
    console.log(result);
  });

Find one and delete

Deletes the first document that matches the criteria, returns that document.

  db.collection('your_db').findOneAndDelete({query_object}).then((result) => {
    console.log(result);
  });

Mongodb: Count


const {MongoClient, ObjectId} = require('mongodb');

const URL = 'mongodb://localhost:27017/<my_app>';

MongoClient.connect(URL, (err, db) => {

if (err) {
return console.log('Unable to connect to mongo db.', err);
}

console.log('Connected to mongo db.');

db.collection('<my_table>').find({
   // Object search criteria as an object    // {
   // attribute1: atribute1Value,
   // ...
   // attributeN: atributeNValue
   // }
}).count().then((count) => {
console.log(JSON.stringify(todos, undefined, 2));

}, (err) => {
console.log('unable to fetch todos', err);
});
});

miércoles, 6 de septiembre de 2017

Mongodb: Insert

Insert

const {MongoClient} = require('mongodb');

const URL = 'mongodb://localhost:27017/<my_app>';

MongoClient.connect(URL, (err, db) => {

if (err) {
return console.log('Unable to connect to mongo db.');
}

console.log('Connected to mongo db.');

db.collection('<my_table>').insertOne({
// Your object
}, (err, result) => {
if (err) {
return console.log('Unnable to insert todo.', err);
}

console.log(JSON.stringify(result.ops, undefined, 2)); // ops stores all the docs inserted
});

db.close();
});


Mongodb: Fetch

Find all

const {MongoClient, ObjectId} = require('mongodb');

const URL = 'mongodb://localhost:27017/<my_app>';

MongoClient.connect(URL, (err, db) => {

if (err) {
return console.log('Unable to connect to mongo db.', err);
}

console.log('Connected to mongo db.');

db.collection('<my_table>').find().toArray().then((docs) => {
console.log(JSON.stringify(todos, undefined, 2));

}, (err) => {
console.log('unable to fetch todos', err);
});
});

Perform search

const {MongoClient, ObjectId} = require('mongodb');

const URL = 'mongodb://localhost:27017/<my_app>';

MongoClient.connect(URL, (err, db) => {

if (err) {
return console.log('Unable to connect to mongo db.', err);
}

console.log('Connected to mongo db.');

db.collection('<my_table>').find({
   // Object search criteria as an object    // {
   // attribute1: atribute1Value,
   // ...
   // attributeN: atributeNValue
   // }
}).toArray().then((docs) => {
console.log(JSON.stringify(todos, undefined, 2));

}, (err) => {
console.log('unable to fetch todos', err);
});
});

Search by id

const {MongoClient, ObjectId} = require('mongodb');

...

db.collection('<my_table>').find({
   // Object search criteria as an object    // {
   // _id: new ObjectID(<objects_id>)
   // }
}).toArray().then((docs) => {
console.log(JSON.stringify(todos, undefined, 2));

}, (err) => {
console.log('unable to fetch todos', err);
});

});