1. 程式人生 > >MongoDB-查詢文件

MongoDB-查詢文件

var documents = new BsonDocument[]
{
    new BsonDocument
    {
        { "item", "journal" },
        { "qty", 25 },
        { "size", new BsonDocument { { "h", 14 }, { "w", 21 }, {  "uom", "cm"} } },
        { "status", "A" }
    },
    new BsonDocument
    {
        { "item", "notebook" },
        { "qty", 50 },
        { "size", new BsonDocument { { "h",  8.5 }, { "w", 11 }, {  "uom", "in"} } },
        { "status", "A" }
    },
    new BsonDocument
    {
        { "item", "paper" },
        { "qty", 100 },
        { "size", new BsonDocument { { "h",  8.5 }, { "w", 11 }, {  "uom", "in"} } },
        { "status", "D" }
    },
    new BsonDocument
    {
        { "item", "planner" },
        { "qty", 75 },
        { "size", new BsonDocument { { "h", 22.85 }, { "w", 30  }, {  "uom", "cm"} } },
        { "status", "D" }
    },
    new BsonDocument
    {
        { "item", "postcard" },
        { "qty", 45 },
        { "size", new BsonDocument { { "h", 10 }, { "w", 15.25 }, {  "uom", "cm"} } },
        { "status", "A" }
    },
};
collection.InsertMany(documents);

查詢集合中的全部文件

若要選擇集合中的所有文件,請將空文件作為查詢篩選器引數傳遞給find方法。查詢篩選器引數確定選擇標準:

var filter = Builders<BsonDocument>.Filter.Empty;
var result = collection.Find(filter).ToList();

此操作對應於以下SQL語句:

SELECT * FROM inventory

指定相等條件

指定相等條件,使用Eq方法構建一個過濾器

Builders<BsonDocument>.Filter.Eq(<field>, <value>);

以下示例查詢 inventory集合的全部文件,條件是 status="D":

var filter = Builders<BsonDocument>.Filter.Eq("status", "D");
var result = collection.Find(filter).ToList();

此操作對應於以下SQL語句:

SELECT * FROM inventory WHERE status = "D"

使用查詢操作符指定條件

除了相等過濾器,MongoDB還提供了多種查詢操作符來生成過濾器,以下示例使用 FilterDefinitionBuilder 方法建立文件的過濾器:

var builder = Builders<BsonDocument>.Filter;
builder.And(builder.Eq(<field1>, <value1>), builder.Lt(<field2>, <value2>));

The following example retrieves all documents from the inventory collection where status equals either "A" or "D":

var filter = Builders<BsonDocument>.Filter.In("status", new[] { "A", "D" });
var result = collection.Find(filter).ToList();

NOTE

Although you can express this query using the $or operator, use the $in operator rather than the $oroperator when performing equality checks on the same field.

The operation corresponds to the following SQL statement:

SELECT * FROM inventory WHERE status in ("A", "D")

Refer to the Query and Projection Operators document for the complete list of MongoDB query operators.

Specify AND Conditions

A compound query can specify conditions for more than one field in the collection’s documents. Implicitly, a logical AND conjunction connects the clauses of a compound query so that the query selects the documents in the collection that match all the conditions.

The following example retrieves all documents in the inventory collection where the status equals "A" andqty is less than ($lt30:

var builder = Builders<BsonDocument>.Filter;
var filter = builder.And(builder.Eq("status", "A"), builder.Lt("qty", 30));
var result = collection.Find(filter).ToList();

The operation corresponds to the following SQL statement:

SELECT * FROM inventory WHERE status = "A" AND qty < 30

See comparison operators for other MongoDB comparison operators.

Specify OR Conditions

Using the $or operator, you can specify a compound query that joins each clause with a logical OR conjunction so that the query selects the documents in the collection that match at least one condition.

The following example retrieves all documents in the collection where the status equals "A" or qty is less than ($lt30:

var builder = Builders<BsonDocument>.Filter;
var filter = builder.Or(builder.Eq("status", "A"), builder.Lt("qty", 30));
var result = collection.Find(filter).ToList();

The operation corresponds to the following SQL statement:

SELECT * FROM inventory WHERE status = "A" OR qty < 30

NOTE

Specify AND as well as OR Conditions

In the following example, the compound query document selects all documents in the collection where the status equals "A" and either qty is less than ($lt30 or item starts with the character p:

var builder = Builders<BsonDocument>.Filter;
var filter = builder.And(
    builder.Eq("status", "A"),
    builder.Or(builder.Lt("qty", 30), builder.Regex("item", new BsonRegularExpression("^p"))));
var result = collection.Find(filter).ToList();

The operation corresponds to the following SQL statement:

SELECT * FROM inventory WHERE status = "A" AND ( qty < 30 OR item LIKE "p%")

NOTE

MongoDB supports regular expressions $regex queries to perform string pattern matches.

相關推薦

MongoDB-查詢

var documents = new BsonDocument[] { new BsonDocument { { "item", "journal" }, { "qty", 25 }, { "size",

mongodb集合中查詢

查詢所有 db.persons.find({},{name:1,country:1}) 第一個{} 是查詢器,裡面放需要查詢的條件,第二個{} 是顯示欄位,就是需要查詢的欄位。1為顯示,0為不顯示。

MongoDB原生態API使用 + 查詢並顯示指定欄位

一、pom依賴<properties> <mongodb.version>3.6.3</mongodb.version> </properties> <dependencies> <dependency&g

查詢MongoDB的List欄位

  Collction Cbettwen含有多級子文件,其中dataList是List型,含有多個字串,每個字串由多個數字組成。需要找出符合如下條件的字串:第1個數字大於6154並小於等於6155。   Cbettwen的某個文件如下:   符合要求的字串為:

【轉】MongoDB配置說明

details 5.0 key 第一個 pen log data 端口 this 啟動MongoDB有2種方式,一是直接指定配置參數,二是指定配置文件。這裏先介紹配置文件,啟動方式如下: mongod --config /etc/mongodb.conf 配置如下:ve

MongoDB基本操作

lis 微軟 文件 mongod local 操作 簡單的 height art MongoDB中主要的文件操作有put、get、list、search幾種。能夠非常方便地進行文件存儲於查找,下面是一個簡單的演示樣例。 1、利用dd命令生成要求大小隨機文件 2

linux命令查詢內容

輸出 sed linu pla 斜杠 表示 inux -s -- 1.查詢文件裏的某一行 sed -n ‘1000p‘ 1.txt --查詢1.txt的第1000行 2.查詢1到5行 sed -n ‘1,5p‘ 1.txt ---查詢1.txt的第1行到

數據庫查詢夾下的

storage long ted 路徑 move cep catch getcount 數據庫查詢 MediaProvider 操作。 1. 通過data查詢出某個路徑ID,然後查詢其Parent未此ID的所有文件。 { Log.e("zcxpre","rowId

MongoDB監控

關閉mongodb服務 ./mongo --port 20000 use admin db.shutdownServer() 重啟MongoDB庫 cd /home/local/mongodb/bin/ ./mongod -f /home/local/mongodb/conf/con

MongoDB索引翻譯(一)

索引 索引讓MongoDB有效的執行查詢語句,沒有索引MongoDB必須對collection中的每個文件掃描來選擇複合條件的條件的。如果對於一個查詢存在合適的索引,MongoDB能利用索引限制檢查的文件數。 索引是一種特殊的資料結構,以簡單的遍歷形式儲

node中使用mongoDB定義並建立索引檢視的解決方法

基於nodeJS建立一個Express站點後,需要連線MongoDB資料庫,此時需要先定義文件,再建立索引(index)檢視,使資料庫的內容能夠展示在網站中。對於怎麼建立Express站點,怎麼連線到MongoDB庫,網上有很多相關資料。 首先定義文

mongodb概念--、集合、資料庫、shell

Mongodb不是關係型資料庫,對於使用關係型資料庫較多的我們,mongodb的一些基本概念需要好好的理解。在理解這些概念的時候,我是以與關係型資料庫等對比的方式來理解的,這樣有助對概念的更好理解。 1.文件   文件時mongodb中資料的基本單元,類似關係型資料庫中的行

mongodb處理--常用的mongoose方法

如同SQL資料庫中2張表有主外關係,Mongoose將2個Document的巢狀叫做Sub-Docs(子文件) 簡單的說就是一個Document巢狀另外一個Document或者Documents: varChildSchema1=newSchema({name:Strin

MongoDB插入

文件的資料結構和JSON基本一樣。所有儲存在集合中的資料都是BSON格式。BSON是一種類json的一種二進位制形式的儲存格式,簡稱Binary JSON。 語法 MongoDB使用inse

Spring Data MongoDB 五:進階查詢(分頁、Morphia)(二)

一.簡介      SpringData  MongoDB提供了org.springframework.data.mongodb.core.MongoTemplate對MongoDB的find

mongoDB內嵌查詢

查詢集合中陣列的其中一個元素: 最近在工作中遇到了這樣的一個問題: 如何取出集合中的一個數組中的一個元素?由於本人是剛剛接觸到mongoDB,經過一番查詢最終還是解決了       在需要對陣列中的值

學習MongoDB 五: MongoDB查詢(陣列、內嵌)(二)

一、簡介         我們上一篇介紹了db.collection.find()可以實現根據條件查詢和指定使用投影運算子返回的欄位省略此引數返回匹配文件中的所有欄位,我們今天介紹了對陣列和內嵌文件

mongodb查詢嵌入/巢狀

這裡演示如何使用:db.collection.find()方法對嵌入/巢狀文件的查詢操作的示例。 此頁面上的示例使用inventory集合。要填充庫存(inventory)集合以準備一些資料,請執行以下命令: db.inventory.insertMany( [ { item: "jour

MongoDB 內嵌查詢

java查詢程式碼 /** * 查詢教師年齡 30 歲 且所帶 年齡為 20 歲且來自 china 學生的教師 */ @Test public void queryTest(){ BasicDBList basicDBList=new BasicDBList

Spring Data MongoDB 三:基本查詢(Query、BasicQuery)(一)

一.簡介      Spring Data  MongoDB提供了org.springframework.data.mongodb.core.MongoTemplate對MongoDB的CRUD的操作,上一篇我們介紹了對MongoDB的新增和刪除, 今天我們