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);
});
});
No hay comentarios:
Publicar un comentario