Install MongoDB on Mac and easy to use

Source: Internet
Author: User
Tags brew install mongodb install mongodb

The beginning of a rough study of the next node, which has not played for several months, suddenly found that has been forgotten, or simply record the basic knowledge, easy to use when the data search.

One, MongoDB installation

If you have brew installed on your Mac, you can execute the command directly

Brew install MongoDB, often added sudo in Mac systems because of the need to write permissions

And then wait a few minutes to install it.

1. MongoDB Start

First terminal window input sudo mongod--config/usr/local/etc/mongod.conf

Then enter MONGO in the second terminal window

2. MongoDB off

Mode one: Press Control+c directly in the first window

Mode two: In the second window switch database to admin use admin and then execute db.shutdownserver ({force:true});

3. MongoDB Repair

Sometimes MongoDB will be unable to start because of some errors, it should be repaired

Mongod--repair

Second, MongoDB commonly used statements

1, Query library, query table

Show dbs-querying all the databases

Show collections-query all data tables under the current database

2. Building and deleting libraries

Use mydbs-to create a database called Mydbs, which is switched to this database when the inventory is in the

Use Mydbs

Db.dropdatabase (); -These two sentences are to delete this database

3. Construction and deletion of tables

Table operations are to go first to a database, using the Use method

Db.myTable.insert ({name: ' HF ', age:20}); -created a table in MongoDB when the data was inserted, and a data table named MyTable was created.

Db.myTable.drop (); -delete mytable This data sheet

4, the single list of additions and deletions to change

Db.myTable.insert ({name: ' hahaha ', age:12}); -New

Db.myTable.update ({name: ' HF '},{$set: {age:25}})-Modify, the action here is equivalent to the update myTable set in the relational database, age = + WHERE name = ' HF '

Db.myTable.remove ({name: ' HF '}); -delete, which is equivalent to delete myTable in the relational database where name = ' HF '

5. Enquiry

Db.myTable.find (); -Query all data in the MyTable

Db.myTable.find (). Sort ({age:1})-based on age ascending

Db.myTable.find (). Count (); -Query

Third, Mongoose and its basic use

because MongoDB is easy to get started, it is often used as a database for Nodejs, while Mongoose is a tool library for Nodejs in the operation of MongoDB. There are several kinds of links in mongoose , such as the schema, Model, entity, 3 kinds of operation classes, in which the schema is an entity in the form of data type, it does not have the ability to manipulate the database. However, it defines the names and types of elements in the Operation data table, and model is the entity of a single table, which instantiates a table with the name of the table and the type labeled in the schema, and the last entity can be considered an instance object of a single row in the table. You can do a simple new check-and-revise operation on a single line.

1, the establishment of links and schema, Model

varMongoose =require (' Mongoose ');/*start getting the database connection, which is partially fixed, * where MyInfo is the name of the database to manipulate*/varcon = Mongoose.connect (' MongoDB://localhost/myinfo ');/*Get Mongoose Global schema object for easy instantiation of multiple tables*/varSche =Mongoose. Schema;/*build the schema of the desired table, where a file of the Stu table is built*/varStuschema =NewSche ({name:string, age:number});/*model models required for building tables*/varStumodel = Con.model (' Stu ', stuschema);

2. New

Way One:

/* New, new operations need to build a single instance of a table */ var New Stumodel ({name: ' HF ', Age:2}); Stuentity.save (function(err) {...});

Way two:

new, new through model */ var json = {name: ' HF ', age:1function(Error) {...})

2. Modification

var param = {name: ' HF '}; var update = {$set: {name: ' SFA ', age:26function(Error) {...})

3. Delete

var param =function(Error) {...})

Install MongoDB on Mac and simple use

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.