Mongo DB 3

239 阅读1分钟

And操作符

  • {$and: [{expression},{expression}]}
> db.aircraft.find({$and: [{capacity : 124},{range: {$gt: 6000}}]}))

Or操作符

db.aircraft.find({$or: [{capacity : 124},{range: {$gt: 6000}}]})

内嵌document的查询

  • location算是departure的内嵌document
  • "departure.location" 来access
{
        "_id" : ObjectId("619380ca78d1812dda203376"),
        "type" : "International",        
        "departure" : {
                "code" : "MUC",
                "city" : "Munich",
                "country" : "Germany",
                "runwayLength" : 4000,
                "location" : {
                        "type" : "Point",
                        "coordinates" : [
                                11.7,
                                48.3
                        ]
                }
        }
}

示例

以下4个document, 关注address字段:

//1: address是object
{
    "name": "Mary",
    "address": {
        "city": "Paris",
        "country": "France"
    }
}
//2: address是string
{ "name": "Luca", "address": "Bucharest, Romania, 110022" }
//3: address是null
{ "name": "Anna", "address": null }
//4: address缺失
{ "name": "Gunter" }

处理null

查询,address为null,的document

  • 方法一:用和null判等,会筛选出以下两种情况:
    • address的值为null
    • 缺少address字段
db.crew.find({address: null})
  • 方法二: 用$type去筛选address为null挺好的不会筛选到address字段缺失的那些document
//type的number
db.crew.find({address: { $type: 10 }})
//type的alias
db.crew.find({address: { $type: "null" }})
> db.flights.find({aircraftCode: {$exists: true}},{aircraftCode : 1})
{ "_id" : ObjectId("619380ca78d1812dda203374"), "aircraftCode" : "1b7ad0de-5836-489b-9791-5a81a51cdb81" }
{ "_id" : ObjectId("619380ca78d1812dda203376"), "aircraftCode" : null }
{ "_id" : ObjectId("619380ca78d1812dda203377"), "aircraftCode" : "eede6be6-f716-4e2e-bf81-885f0a16a50c" }

> db.flights.find({aircraftCode: {$exists: true, $type:"string"}},{aircraftCode : 1})
{ "_id" : ObjectId("619380ca78d1812dda203374"), "aircraftCode" : "1b7ad0de-5836-489b-9791-5a81a51cdb81" }
{ "_id" : ObjectId("619380ca78d1812dda203377"), "aircraftCode" : "eede6be6-f716-4e2e-bf81-885f0a16a50c" }

处理字段缺失

$exists操作符:查询,缺少address字段,的document

db.crew.find({address: { $exists: false }})

type操作符

查询,address字段为某种type,的document

  • 有时候一个同名的字段如address,其type可以有多种,于是需要查询某种type的某个字段
typenumberalias
Double1"double"
String2"string"
Object3"object"
Array4"array"
Boolean8"bool"
Date9"date"
10"null"
64-bit integer18"long"
//type的number
db.crew.find({address: { $type: 3 }})
//type的alias
db.crew.find({address: { $type: "object" }})

free text查询

三个关键内容:

  • text index,必须要有,才能做free text查询
  • $text
  • $meta

创建index "text"

  • 支持,string类型,的字段
  • 支持,元素为string的array类型,的字段
//name和skills,为希望实施free text查询,的字段名称
> db.crew.createIndex({"name": "text", "skills": "text"})
{
        "numIndexesBefore" : 1,
        "numIndexesAfter" : 2,
        "createdCollectionAutomatically" : false,
        "ok" : 1
}
  • 查看已有Index
> db.flights.getIndexes()
[ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_" } ]

$text操作符

  • 会查询出:
    • 包含technical或者Anna的document
    • 不论是string字段,还是array的某个string元素
> db.crew.find({$text: {$search: "technical Anna"}}).pretty()
{
        "_id" : ObjectId("619380fe204711818273f99d"),
        "name" : "Anna Smith",
        "skills" : [
                "technical",
                "management"
        ],
        "address" : {
                "city" : "Bucharest",
                "country" : "Romania"
        }
}
{
        "_id" : ObjectId("619380fe204711818273f99c"),
        "name" : "Andrei Luca",
        "skills" : [
                "technical",
                "management"
        ],
        "address" : {
                "city" : "Bucharest",
                "country" : "Romania"
        }
}

$meta操作符

  • 把free text查询出来的document按相关性排序:
    • 写一个projection 对象: {score: {$meta: "textScore"}},计算出了一个新字段score
    • 结构看上去就是:find({$text: {$search: 要查询的内容}}, projection对象).sort(projection对象)
> db.crew.find({$text: {$search: "technical Anna"}}, {score: {$meta: "textScore"}}).sort({score: {$meta: "textScore"}}).pretty()
{
        "_id" : ObjectId("619380fe204711818273f99d"),
        "name" : "Anna Smith",
        "skills" : [
                "technical",
                "management"
        ],
        "address" : {
                "city" : "Bucharest",
                "country" : "Romania"
        },
        "score" : 1.75
}
{
        "_id" : ObjectId("619380fe204711818273f99c"),
        "name" : "Andrei Luca",
        "skills" : [
                "technical",
                "management"
        ],
        "address" : {
                "city" : "Bucharest",
                "country" : "Romania"
        },
        "score" : 1
}