Hướng dẫn mongodb w3school
Creating a DatabaseTo create a database in MongoDB, start by creating a MongoClient object, then specify a connection URL with the correct ip address and the name of the database you want to create. MongoDB will create the database if it does not exist, and make a connection to it. ExampleCreate a database called "mydb": var MongoClient = require('mongodb').MongoClient;
MongoClient.connect(url, function(err, db) { Run example » Save the code above in a file called "demo_create_mongo_db.js" and run the file: Run "demo_create_mongo_db.js" C:\Users\Your Name>node demo_create_mongo_db.js Which will give you this result: Important: In MongoDB, a database is not created until it gets content! MongoDB waits until you have created a collection (table), with at least one document (record) before it actually creates the database (and collection). In MongoDB we use the find and findOne methods to find data in a collection. Just like the SELECT statement is used to find data in a table in a MySQL database. Find OneTo select data from a collection in MongoDB, we can use the The The first parameter of the ExampleFind the first document in the customers collection: var MongoClient = require('mongodb').MongoClient; MongoClient.connect(url, function(err, db) { Run example » Save the code above in a file called "demo_mongodb_findone.js" and run the file: Run "demo_mongodb_findone.js" C:\Users\Your Name>node demo_mongodb_findone.js Which will give you this result: Find AllTo select data from a table in MongoDB, we can also use the The The first parameter of the No parameters in the find() method gives you the same result as SELECT * in MySQL. ExampleFind all documents in the customers collection: var MongoClient = require('mongodb').MongoClient; MongoClient.connect(url, function(err, db) { Run example » Save the code above in a file called "demo_mongodb_find.js" and run the file: Run "demo_mongodb_find.js" C:\Users\Your Name>node demo_mongodb_find.js Which will give you this result: [ Find SomeThe second parameter of the This parameter is optional, and if omitted, all fields will be included in the result. ExampleReturn the fields "name" and "address" of all documents in the customers collection: var MongoClient =
require('mongodb').MongoClient; MongoClient.connect(url, function(err, db) { Run example » Save the code above in a file called "demo_mongodb_find_fields.js" and run the file: Run "demo_mongodb_find_fields.js" C:\Users\Your Name>node demo_mongodb_find_fields.js Which will give you this result: [ You are not allowed to specify both 0 and 1 values in the same object (except if one of the fields is the _id field). If you specify a field with the value 0, all other fields get the value 1, and vice versa: ExampleThis example will exclude "address" from the result: var MongoClient =
require('mongodb').MongoClient; MongoClient.connect(url, function(err, db) { Run example » To exclude the _id field, you must set its value to 0: ExampleThis example will return only the "name" field: var MongoClient = require('mongodb').MongoClient; MongoClient.connect(url, function(err, db) { Run example » ExampleThis example will give you the same result as the first example; return all fields except the _id field: var MongoClient = require('mongodb').MongoClient; MongoClient.connect(url, function(err, db) { Run example » ExampleYou get an error if you specify both 0 and 1 values in the same object (except if one of the fields is the _id field): var MongoClient = require('mongodb').MongoClient; MongoClient.connect(url, function(err, db) { Run example » The Result ObjectAs you can see from the result of the example above, the result can be converted into an array containing each document as an object. To return e.g. the address of the third document, just refer to the third array object's address property: ExampleReturn the address of the third document: console.log(result[2].address); Which will produce this result: |