MongoDB常用操作--集合2

來源:互聯網
上載者:User

標籤:mit   技術   name   大於   one   特定   log   count()   god   

1.查詢集合中的文檔,可以使用命令 db.集合名稱.find({條件}),或者使用 db.集合名稱.findOne() 查詢第一個文檔

 

2.查詢集合中的文檔,返回某些特定的索引值



3.查詢集合中的文檔 ,使用條件運算式(<, <=, >, >=,!=)

//大於: field > value
db.collection.find({field:{$gt:value}});

//小於: field < value
db.collection.find({field:{$lt:value}});

//大於等於: field >= value
db.collection.find({field:{$gte:value}});

//小於等於: field <= value
db.collection.find({field:{$lte:value}});

//不等於: field != value
db.collection.find({field:{$ne:value}});

 

4.查詢集合中的文檔 ,統計(count)、排序(sort)、分頁(skip、limit)

db.customer.count();
db.customer.find().count();
db.customer.find({age:{$lt:5}}).count();
db.customer.find().sort({age:1});
db.customer.find().skip(2).limit(3);
db.customer.find().sort({age:-1}).skip(2).limit(3);
db.customer.find().sort({age:-1}).skip(2).limit(3).count();
db.customer.find().sort({age:-1}).skip(2).limit(3).count(0);
db.customer.find().sort({age:-1}).skip(2).limit(3).count(1);

 

5.查詢集合中的文檔 ,$all主要用來查詢數組中的內含項目關聯性,查詢條件中只要有一個不包含就不返回

 

6.查詢集合中的文檔 ,$in,類似於關係型資料庫中的IN

 

 7.查詢集合中的文檔 ,$nin,與$in相反

 

 8.查詢集合中的文檔 ,$or,相當於關係型資料庫中的OR,表示或者的關係,例如查詢name為user2或者age為3的文檔,
命令為: db.customer.find({$or:[{name:”user2”},{age:3}]})

 

9.查詢集合中的文檔 ,$nor,表示根據條件過濾掉某些資料,例如查詢name不是user2,age不是3的文檔,
命令為: db.customer.find({$nor:[{name:”user2”},{age:3}]})

 

10.查詢集合中的文檔 ,$exists,用於查詢集合中存在某個鍵的文檔或不存在某個鍵的文檔,例如查詢customer集合中存在name鍵的所有文檔,可以使用 db.customer.find({name:{$exists:1}}),
$exists:1表示真,指存在
$exists:0表示假,指不存在

 

11.查詢集合中的文檔 ,類似於關係型資料庫,mongodb中也有遊標的概念

 

MongoDB常用操作--集合2

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.