var mongoose = require('mongoose');
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost:27017/TodoApp');
var TodoSchema = mongoose.model('Todo', {
text: {
type: String,
required: true,
minlength: 1,
trim: true
},
completed: {
type: Boolean,
default: false
},
completedAt: {
type: Number,
default: null
}
});
var newTodo = new TodoSchema({
text: 'Cook dinner',
});
newTodo.save().then((doc) => {
console.log(JSON.stringify(doc, undefined, 2));
}, (error) => {
console.log('Unable to save: ', error);
});
lunes, 2 de octubre de 2017
Moongose: Start
var mongoose = require('mongoose');
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost:27017/<your_app>');
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost:27017/<your_app>');
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) => {
});
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);
});
// 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();
});
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)