常用的mongodb命令
Jay Zangwill
·
2017-04-10
·
via Jay Zangwill
下面是一些我常用的mongodb命令,供自己备忘
- show dbs (列出所有数据库)
- use [database name] (数据库的切换)
- show collections (查看当前使用数据库有哪些表)
- db.[表名].find() (查找当前表下所有数据)
- db.[表名].findOne({‘key’:value}) (查找一条符合查询条件的数据)
- db.[表名].drop() (删除当前表)
- db.[表名].move({‘key’:value}) (根据条件删除数据)
- help (输出mongodb的帮助)
- db help() (数据库的帮助命令)
关于mongodb更多
node与mongodb交互
1 2 3 4 5 6 7 8
| // 插入操作 function insertDocuments(db, data, callback) { let collection = db.collection('documents'); collection.insertOne(data, (err, result) => { assert.equal(err, null); callback(result); }); }
|
1 2 3 4 5 6 7 8 9 10 11 12
| // 查找操作 function findDocuments(db, callback, search) { search = search || {}; // Get the documents collection let collection = db.collection('documents'); // Find some documents collection.find(search).toArray((err, docs) => { assert.equal(err, null); callback(docs); }); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| // 更新操作 function updateDocument(db, userinfo, money, callback) {
// Get the documents collection let collection = db.collection('documents');
collection.updateOne({ idCard: userinfo }, { $set: { money: money } }, (err, result) => { assert.equal(err, null); callback(result); }); }
|
1 2 3 4 5 6 7
| function deleteDocument(db, userinfo, callback) { // Get the documents collection let collection = db.collection('documents'); collection.deleteOne({ idCard: userinfo }, (err, result) => { callback(result); }); }
|
更多node与mongodb交互
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。