viernes, 29 de septiembre de 2017
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();
});
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);
});
});
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);
});
});
martes, 29 de agosto de 2017
Mongodb: Start server
1) Navigate to the folder where your mongo executables are (usually placed on user's folder)
2) Starts the server, creates an active connection to manipulate data:
./mongod --dbpath ~/mongo-data // Or whatever your folder name is
3) On another tab run:
./mongo // connects to the database
2) Starts the server, creates an active connection to manipulate data:
./mongod --dbpath ~/mongo-data // Or whatever your folder name is
3) On another tab run:
./mongo // connects to the database
Mongodb: Object destructuring
Obtain variables from object properties.
var user = {name: ‘Andrew’, age: 25};var {name} = user;
Also
const {MongoClient} = require('mongodb');
is equal to
const MongoClient = require('mongodb').MongoClient;
var user = {name: ‘Andrew’, age: 25};var {name} = user;
Also
const {MongoClient} = require('mongodb');
is equal to
const MongoClient = require('mongodb').MongoClient;
Suscribirse a:
Entradas (Atom)