mongodb練習記錄

學習鏈接

https://segmentfault.com/a/1190000015983748

 

db.getCollection('foo').find().pretty();
db.getCollection('foo').findOne();
db.getCollection('foo').find({"hello":"world"});
db.getCollection('foo').find({"hello":{$ne:"world"}});
//$lt;<<br>db.getCollection('user_test').find({"age":{$lt:"28"}});//查詢集合爲user_test,條件爲年齡小於28的記錄
//$lte; <=
db.getCollection('user_test').find({"age":{$lt:"28"}});//查詢集合爲user_test,條件爲年齡小於等於28的記錄
//$gt; >
db.getCollection('user_test').find({"age":{$gt:"28"}});//查詢集合爲user_test,條件爲年齡大於28的記錄
//$gte; >=
db.getCollection('user_test').find({"age":{$gte:"28"}});//查詢集合爲user_test,條件爲年齡大於等於28的記錄
//$ne; !=
db.getCollection('user_test').find({"age":{$ne:"28"}});//查詢集合爲user_test,條件爲年齡不等於28的記錄

db.getCollection("foo").find({"hello":"world","hello":"world"});
select * from foo where hello="world" AND hello = "world";

db.getCollection("foo").find({$or:[{"hello":"world"},{"hello":"world2"}]});

db.getCollection("foo").find({"hello":"world",$or:[{"hello":"world"}]});


//mysql like
db.getCollection("foo").find({"hello":{$regex:"world2"}});
db.getCollection("fooo").find({"hello":/world2/});
db.getCollection("foo").find({"hello":/^(world)|(world2)$/i});

db.getCollection("user_test").find().sort({"name":-1}).skip(0).limit(10);//查詢集合爲user_test,排序key爲name排序方式爲-1倒序,skip(0).limit(10)從0到10
//插入記錄
db.user_test.insert({name: '張三',
 

   age:'18',
    sex:'男',
    favorite:['mongodb', 'database', 'NoSQL'],
})//插入集合爲user_test中
db.user_test.insert({name: '張三',
    age:'18',
    sex:'男',
    favorite:{"sports":{"ball":"basketball","games":"fire"},"books":{"animal":"動物世界","cars":"汽車世界"}},
})
db.user_test.insert({name: '張三',
    age:'18',
    sex:'男',
    favorite:{"sports":{"ball":"basketball","games":"fire"},"books":{"animal":"動物世界","cars":"汽車世界"}},
})
db.user_test.insert({name: '張三',
    age:'18',
    sex:'男',
    favorite:{"sports":{"ball":"basketball","games":"fire"},"books":{"animal":"動物世界","cars":"汽車世界"}},
})

db.user_test.insert({name: '張三',
    age:'18',
    sex:'男',
    favorite:{"sports":"running,swiming"},
})

db.user_test.update({"name":"張三"},{$set:{"favorite.sports.ball":"footerball"}});//修改一條name爲張三的favorite.sports.ball值爲footerball
db.user_test.update({"name":"張三"},{$set:{"favorite.sports.ball":"footerball"}},{multi:true});//修改所有name爲張三的favorite.sports.ball值爲footerball
db.user_test.remove({"name":"張三"});//刪除所有name爲張三的記錄
db.user_test.remove({"name":"張三"},1);//刪除一條name爲張三的記錄,remove的第二個參數爲1時刪除一條

發佈了14 篇原創文章 · 獲贊 24 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章