Mongo 索引和搜索

MongoDB stores data in flexible, JSON-like documents, meaning fields can vary from document to document and data structure can be changed over time.

help

  • > help
    MongoDB help
  • > db.help()
  • >db.mycoll.help() 指定的 mycoll 集合的方法
    help on collection methods
  • db.d_320100.getIndexes 查当前索引
    db.d_320100.getIndexes()

示例

  • 以 PAIBO 标准为示例。
  • 指定场所是否有场所心跳数据?
    由于场所心跳UDP数据包的内容就是场所编码,因此直接搜索所存储的二进制数据即可:MongoDB Shell: find by BinData
    场所编码 32119121000149(base64值:MzIxMTkxMjEwMDAxNDk=)最近一条场所心跳数据:
> db.d_321100_XXXXXXXXX.find( {"VALUE": new BinData(0,"MzIxMTkxMjEwMDAxNDk=") } ).sort({TM:-1}).limit(1)
{ "_id" : ObjectId("5c46c6cda1b9402f388908ad"), "SN" : "321100---0-7621138-SITESTATUS-0", "TM" : NumberLong("1548142285103"), "UTM" : NumberLong("1548142285518"), "FLAG" : NumberLong(200), "TYPE" : 105, "VALUE" : BinData(0,"MzIxMTkxMjEwMDAxNDk=") }
pretty 格式示例
  • 场所编码 32119121000149(位于 SN 中),终端上下线(TYPE 101)的最近一条格式化数据
> db.d_321100_XXXXXXXXX.find( {SN: { $regex: /-32119121000149-/ }, TYPE: 101 } ).sort({TM:-1}).limit(1).pretty()
{
    "_id" : ObjectId("5c46f3eca1b9402f38b06e8f"),
    "SN" : "321100-32119121000149-XXXXXXXXX749D79E98850-1548152023-7876037-WA_SOURCE_FJ_0001",
    "TM" : NumberLong("1548153836614"),
    "UTM" : NumberLong("1548153836707"),
    "FLAG" : NumberLong(200),
    "TYPE" : 101,
    "VALUE" : BinData(0,"Vjk2dDVyd1VRdmp2V0VNcjVRPT0=")
}
注:示例中的 VALUE 字段值经过裁剪。即使未经裁剪,也是加密过的数据。
  • 场所编码 32119121000149(位于 SN 中),终端上下线(TYPE 101)或者虚拟身份(102)的最近一条数据:
> db.d_321100_XXXXXXXXX.find( {SN: { $regex: /-32119121000149-/ }, TYPE: {$in:[101,102]} } ).sort({TM:-1}).limit(1)
  • 场所编码 32119121000149(位于 SN 中),终端上下线(TYPE 101)或者虚拟身份(102)一共有多少条数据:
> db.d_321100_XXXXXXXXX.find( {SN: { $regex: /-32119121000149-/ }, TYPE: {$in:[101,102]} } ).count()
1580

正则搜索

  • Selects documents where values match a specified regular expression.
  • Provides regular expression capabilities for pattern matching strings in queries. MongoDB uses Perl compatible regular expressions (i.e. “PCRE” ) version 8.41 with UTF-8 support.

文本搜索

  • Text Search
    Performs text search.
  • MongoDB supports query operations that perform a text search of string content. To perform text search, MongoDB uses a text index and the $text operator.
  • A collection can only have one text search index, but that index can cover multiple fields.

方法