MongoDB operations

①Create a database: use dataname

If there is, go to, if not, create

②View the current database: db [note that the new ones are empty here]

③Create an administrator: db.addUser("shuaige", "123456")//Create an account called Handsome Guy for this database with a password of 123456 (but I think maybe I don't understand it well, you can also not do this operation )
If the database is started in authorized mode, then we can perform administrator authentication, and then we can continue to operate

④Check the database list: show dbs [empty can not be displayed, to display the database, you need to insert at least one document

db.items.insert({"name":"yiibai tutorials"})

The default database in MongoDB is: test. Collections/documents will be stored in the test database if you haven't created any databases.

⑤Create a form collection: db.createCollection("users")//Create a collection, that is, a table

⑥Insert data: db.users.insert({user: "admin", password: "123456"})//Add a document to users, that is, a record account admin, password 123456

 

⑦ Delete the database: db.dropDatabase()

Delete the currently selected database, want to see the current database with db. If no database is selected then it will delete the default test database

 

Start mongo in authorized mode

Connect to mongo first:

linux/Mac : mongod -f /mongodb/etc/mongo.conf

windows :mongod --config C:\Users\lg\Desktop\mongodb\mongodb-win32-i386-2.4.5\etc\mongo.conf

Authorization mode to start: mongo --auth or mongo --journal

After entering, you need to verify your identity, so you need to create a global administrator first.

 

⑥Add ordinary users:

> use tank;

switched to db tank

> db.addUser('tank1','test'); //Add a readable and writable user tank1 to the tank database

{

    "_id" : ObjectId("529e5f8474b4c660718a70f3"),

    "user" : "tank1",

    "readOnly" : false,

    "pwd" : "35dd47abff098f5b4f0b567db8edeac5"

}

> db.addUser('tank2','test',true); //Add a read-only user tank2 to the tank database

{

    "user" : "tank2",

    "readOnly" : true,

    "pwd" : "1792916c544d247538ded52e6df7b887",

    "_id" : ObjectId("529e67553992b24438d5e315")

}

> exit //exit

 

 

There are two main ways to use mongodb, one of which is used here: using mongoose

 

  Mongoose is an object model tool for MongoDB. It is a MongoDB nodejs driver developed based on node-mongodb-native and can be executed in an asynchronous environment.

 

At the same time, it is also an object model library for MongoDB operations. It encapsulates some common methods such as adding, deleting, modifying, and querying documents by MongoDB, making it more flexible and simple for NodeJS to operate the Mongodb database.

 

We use Mongoose to create a "collection" and add, delete, modify and query it, we need to use its three attributes: Schema (data attribute model), Model, Entity

 

 

Schema - a database model skeleton stored in the form of a file, which cannot directly lead to the database side, that is to say, it does not have the ability to operate the database, but is only a representation of the database model in the program fragment, which can be said to be data The attribute model (table structure in the traditional sense), or the model skeleton of the "collection".

 

For example, define a Schema:

 

var mongoose = require("mongoose");

 

var TestSchema = new mongoose.Schema({

    name : { type: String },//Attribute name, type is String

    age : { type:Number, default:0 },//Attribute age, the type is Number, the default is 0

    time : { type:Date, default:Date.now },

    email: { type:String,default:''}

});

 

Model - The model generated by Schema construction, in addition to the database skeleton defined by Schema, also has database operation behaviors, similar to the classes that manage database attributes and behaviors.

 

For example, define a Model:

 

var db = mongoose.connect("mongodb://127.0.0.1:27017/test");

 

// create Model

var TestModel = db.model("test1", TestSchema);

Entity - an entity created by Model, save data using the save method, both Model and Entity have operations that can affect the database, but Model is more operational than Entity.

 

For example, define an Entity:

 

var TestEntity = new TestModel ({

       name : "Lenka",

       age  : 36,

       email: "[email protected]"

});

console.log(TestEntity.name); // Lenka

console.log(TestEntity.age); // 36

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326566564&siteId=291194637