You are trying to access MongoDB on the native driver port. For http diagnostic access, add 1000 to the port number
說明數據庫開啓成功.
3.然後新建終端打開mongodb->bin->mongo.默認進入的是test庫.
4.細說插入語句 (insert)
單條插入
var single = {"name":"qimeng","age":4}
db.user.insert(single);
批量插入
for (var i= 0;i<20;i++){db.user.insert(single)}
5.細說查看語句 (find)
db.user.find() //查看全部
db.user.find({"age":{$gt:20}}) //查看年齡>20的...如果$gte.就是>=
db.user.find({"age":{$lt:20}}) //查看年齡<20的...如果$lte.就是<=
db.user.find({"age":{$ne:20}}) //查看年齡!=20的...
db.user.find({"age":20}) //
db.user.find({"name":"qimeng","age":4}) //&&的關係
db.user.find({$or:[{"name":"qimeng"},{"age":4}]}) //||的關係
$nin 直接從官網上接下來的. 反選
-
Syntax: { field: { $nin: [ <value1>, <value2> ... <valueN> ]} }
$nin selects the documents where:
- the field value is not in the specified array or
- the field does not exist.
上面反選明白了.$in 也就不用解釋了.
好吧.還支持正則.正則沒有深入瞭解.略懂略懂.
好吧.其實對於js我也是略懂略懂.學習的都是oc.對於js.還是一點點通而已..不過還是看的明白的.
db.user.find({$where:function(){return this.name=='liu'}})
6.更新操作(update)
update 可以帶4個參數.1.查找條件.2.更新的值,3.true(可加可不加.加了.就變了upsert是mongodb創造的.就是實現有着更新.沒有則添加.),4.true(可加,可不加,加了.就是全部匹配的都更新.不加.默認第一個)
db.user.update({"name":"qimeng",{"age":0}})將會整體更新.也就是更新之後,這條數據.只有自帶的一個_id,和age,其他的都唄清空了
如果只要修改局部的更新.可以用$inc.$set.
$inc.只能修改作用與numbers類型的,並與搜索到的numbers相加.而$set直接替換
db.user.update({"name":"qimeng"},{$inc:{"age":100}})//更新完之後"age":100+原本的數字
db.user.update({"name":"qimeng"},{$set:{"age":100}}) //更新完之後"age":100
7.刪除操作(remove)
db.user.remove()//將所有的數據刪除.謹慎
db.user.remove({"name":"qimeng"})//批量將滿足條件的都刪除了.