佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > MongoDB操作符中的$elemMatch問題

MongoDB操作符中的$elemMatch問題

熱門標簽:網(wǎng)站排名優(yōu)化 服務(wù)外包 呼叫中心市場需求 百度競價排名 鐵路電話系統(tǒng) Linux服務(wù)器 AI電銷 地方門戶網(wǎng)站

問題

如果MongoDB 數(shù)據(jù)庫集合中僅存在一條記錄

{
  "_id" : ObjectId("5e6b4ef546b5f44e5c5b276d"),
  "name" : "趙小明",
  "used_name" : [ 
    "趙明", 
    "趙小朋"
  ],
  "age" : 16,
  "gender" : 0,
  "relatives" : [ 
    {
      "name" : "趙剛",
      "relationship" : 0
    }, 
    {
      "name" : "秀英",
      "relationship" : 1
    }
  ]
}

我們執(zhí)行查詢

db.getCollection('Persion').find({"relatives.name": "趙剛", "relatives.relationship": 1})

此時會得到結(jié)果嗎?

最開始我想當然的以為是不會出現(xiàn)結(jié)果的,但結(jié)果往往與期望背道而馳。

什么,一瞬間我陷入了迷茫,Mongo的查詢結(jié)果不是必須都滿足所有條件的嗎?

分析

不信邪的我又嘗試了喜聞樂見的小白查詢

db.getCollection('Persion').find({"name": "趙小明", "age": 18})

這次結(jié)果為空,嗯,這才是我熟悉的Mongo嘛?

那這兩次查詢有啥區(qū)別呢?不同有兩點

  • 是否為二級字段
  • 是否為數(shù)組

那我們將數(shù)據(jù)改為

{
  "_id" : ObjectId("5e6b4ef546b5f44e5c5b276d"),
  "name" : "趙小明",
  "used_name" : [ 
    "趙明", 
    "趙小朋"
  ],
  "age" : 16,
  "gender" : 0,
  "relative" : {
    "name" : "趙剛",
    "relationship" : 0
  }
}

繼續(xù)執(zhí)行查詢

db.getCollection('Persion').find({"relatives.name": "趙剛", "relatives.relationship": 1})

此次結(jié)果為空集

接下來嘗試查詢

db.getCollection('Persion').find({"relatives.name": "趙剛", "relatives.relationship": 0})

此次可得到一條結(jié)果

通過上述兩次查詢基本可以排除二級字段的影響

那就是數(shù)組的原因了,那具體是為什么呢?

將數(shù)據(jù)還原為最初的格式,繼續(xù)進行不同的查詢

db.getCollection('Persion').find({"relatives.name": "趙剛", "relatives.relationship": 2})

此次結(jié)果為空集

那我們可以得到結(jié)論,對于數(shù)組字段,每個查詢條件僅需有數(shù)組中的一項滿足條件即可,而不是數(shù)組中必須存在一項滿足所有查詢條件。

那如果我想達到后面的效果要怎么做呢?

解決

此時,我們需要用到我們今天的主角 $elemMatch ,它的官方定義是這樣的:

The $elemMatch operator matches documents that contain an array field with at least one element that matches all the specified query criteria.

{ field>: { $elemMatch: { query1>, query2>, ... } } }
If you specify only a single condition in the $elemMatch expression, you do not need to use $elemMatch.

You cannot specify a $where expression in an $elemMatch.
You cannot specify a $text query expression in an $elemMatch.

那上邊的查詢我們可以改成

db.getCollection('Persion').find({"relatives":{"$elemMatch":{"name": "趙四", "relationship": 0}}})

此時可以得到結(jié)果,但

db.getCollection('Persion').find({"relatives":{"$elemMatch":{"name": "趙四", "relationship": 1}}})

結(jié)果為空集

結(jié)語

此操作符和索引也有一些不得不說的事,今天就不在這里細說了,之后我會專門總結(jié)一篇有關(guān)MongoDB索引相關(guān)的博客

等不及的看官可以自行百度Google一下。

總結(jié)

到此這篇關(guān)于MongoDB操作符之$elemMatch的文章就介紹到這了,更多相關(guān)MongoDB操作符之$elemMatch內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • MongoDB 管道的介紹及操作符實例
  • 基于MongoDB數(shù)據(jù)庫的數(shù)據(jù)類型和$type操作符詳解

標簽:湖南 衡水 崇左 蘭州 銅川 黃山 湘潭 仙桃

巨人網(wǎng)絡(luò)通訊聲明:本文標題《MongoDB操作符中的$elemMatch問題》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    尚志市| 盘锦市| 通河县| 凤山县| 甘泉县| 寻乌县| 张家港市| 凤城市| 墨竹工卡县| 马关县| 特克斯县| 微山县| 隆化县| 绥宁县| 沭阳县| 南京市| 临汾市| 舞阳县| 镇康县| 剑川县| 乐至县| 腾冲县| 眉山市| 昭觉县| 平武县| 宁化县| 通河县| 临夏县| 海原县| 炎陵县| 新闻| 玛多县| 长泰县| 新民市| 西和县| 大名县| 宜春市| 福泉市| 南宫市| 丰宁| 海淀区|