1. Install mongodb
brew install mongodb
2. Database configuration
1)vim ~/.bash_profile 2)立刻生效 source .bash_profile # mongod db export PATH=/usr/local/Cellar/mongodb/3.4.9/bin:${PATH}
3. Open mongoldb
1) sudo mongod // 开启服务 2) mongo // 链接mongodb
4. Basic database operations
1) sho abs; // 显示数据库 2)use userinfo //如果有则切换到userinfo数据库,如果没有就创建userinfo 3) db //返回userinfo,表示当前userinfo处于准备状态 4) db.users.insert({username:'admin',password:'123'}) //向userinfo中的users集合插入一条数据。如果有合集users则向该集合插入数据,如果没有,则先创建users集合,再插入数据 5)db.users.find().pretty() //显示users集合中的数据,pretty()是格式化 6)db.users.remove({name:'xxx'}) //删除name为'xxx'的记录 7)db.users.remove({}) //删除users集合中的所有数据 8)db.users.drop() //删除users集合 9)db.users.findOne({name:'xxx'}) //查询name为'xxx'的记录,只返回遇到的第一条记录 10)db.users.find() //返回所有name为'xxx'的记录
5. Close the database:
Execute the following command on the command line
use admin; db.shutdownServer();
The above is the detailed content of Introduction to the installation and basic operation of Mongodb in Node. For more information, please follow other related articles on the PHP Chinese website!