mongoDB常用操作指令

2021-10-08 00:21:50 字數 2363 閱讀 5842

簡單操作

// 插入資料

db.student.

insert()

;db.student.

insert([

,,,]

);db.student.

find()

;db.student.

find()

;db.student.

find()

;db.student.

findone()

;db.student.

find()

.count()

;db.student.

find()

;db.student.

update(,

});db.student.

update(,

},);

db.student.

update(,

});//刪除

db.student.

find()

;db.student.

remove()

;// 刪除多條

db.student.

remove(,

true);

//只刪一條

// 命令列改變資料檔案儲存位置

mongod --dbpath e

:\data2

// 命令列匯入json檔案到集合中

mongoimport --db 資料庫名 --collection 集合名--drop --file json檔案絕對路徑 //drop表示清空集合

操作練習

use jozq;

// 使用資料庫,無則建立

db;db.colleges.

insert([

,,,,

]);db.colleges.

find()

;db.colleges.

insert()

;db.colleges.

update(,

});// 只更新一條資料

db.colleges.

update(,

},);

// 條件更新多條資料

db.colleges.

update(,

},);

// 更新name

db.colleges.

update(,

});// 刪除欄位age的內容

db.colleges.

update(,

}});

// 內嵌文件查詢、新增資料、刪除資料 點語法

db.colleges.

find()

;db.colleges.

find()

;db.colleges.

update(,

});db.colleges.

update(,

});db.colleges.

update(,

});//排序

db.colleges.

find()

.sort()

// 先按score公升序排序,再按age排序相同的score,-1為降序

// 大批量插入資料

/*var arr = ;

for(var i =0;i<1000;i++));

} db.demos.insert(arr);

*/db.demos.

find(}

);// 查詢counter大於100小於150的資料

db.demos.

find(}

);// 小於等於

db.demos.

find()

.limit(10

);// 前10條資料

db.demos.

find()

.skip(10

).limit(10

);// 分頁 查詢11到20條資料

// 多集合聯合查詢

var money = db.collection1.

findone()

.money;

db.collection2.

find()

;// 邏輯操作符 查詢小於100或大於200的資料

db.collection3.

find

($or:[}

,}])

;// $inc加運算子 money小於等於500的加20

db.collection3.

updatemany

(money:,}

);

MongoDB筆記(二)常用操作指令

資料庫 database 集合 collection 文件 document db.collectionname.insert docobject db.collectionname.insert docobject 或者更加明確的語義 db.collectionname.insertone doc...

MongoDB 常用指令

mongodb 常用基礎指令 備忘錄。mongod dbpath第一種是linux指令 killall mongod kill 10014 sigterm 第二種是進入mongo資料庫 推薦 use admin switched to db admin db.shutdownserver serve...

mongodb資料庫常用的操作指令

啟動資料庫 net start mongodb 關閉資料庫 net stop mongodb 庫操作 mongo 進入命令模式 use dbname 建立乙個新庫 show dbs 檢視所有庫 db.dropdatabase 刪除資料庫 exit 退出命令模式 集合操作 db.createcolle...