程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> MongoDB常用操作--集合2,mongodb--集合

MongoDB常用操作--集合2,mongodb--集合

編輯:關於PHP編程

MongoDB常用操作--集合2,mongodb--集合


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中也有游標的概念

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved