mongo 随笔1

P版云环境 

自助mongo 数据库集群

sip_host01:PRIMARY> show dbs
admin                    0.000GB
bk-cmdb-adapter          0.031GB
config                   0.000GB
host-evacuation-service  0.000GB
local                    0.046GB
onlinesnapshot-service   0.000GB
order-business           0.000GB
tstack_cmdb_prod         0.002GB
sip_host01:PRIMARY> use tstack_cmdb_prod
switched to db tstack_cmdb_prod
sip_host01:PRIMARY> 

查询  t_inst__Server 这个col(表)中的数据;pretty()  格式化输出

sip_host01:PRIMARY> db.t_inst__Server.find().pretty()  (以下ip已经更改)

{
    "_id" : ObjectId("5caf296d7b16cee38a9ba0c5"),
    "svrCategory" : "VirtualParent",
    "allIp" : [
        "0.0.0.0"
    ],
    "hostName" : "openstack-com09",
    "cloudStatus" : "入云成功",
    "creator" : "admin",
    "ip" : "0.0.0.0",
    "flavorId" : {
        "disk" : "2048",
        "instanceId" : "d2c1566a-6477-45b4-a0c8-9b7f58e5f331",
        "name" : "TENCENTRegion_PM-01",
        "cpu" : "48",
        "ram" : "131072"
    },
    "dept" : {
        "number" : "admin",
        "deptName" : "默认部门",
        "instanceId" : "43979",
        "deptId" : "43979",
        "name" : "默认部门-43979"
    },
    "osId" : {
        "instanceId" : "ab091c65-0a78-4445-9fe1-bbcd2df39953",
        "osVersion" : "3.10.0",
        "name" : "CentOS7.2_3.10.0_3.10.0",
        "osName" : "CentOS7.2"
    },
    "serverId" : "074EE5D1-FEBE-5BA6-B5A3-8436D7B84E83",
    "version" : "1",
    "operator" : [
        "admin"
    ],
    "bakOperator" : [
        "admin"
    ],
    "idcPosId" : {
        "instanceId" : "32f61ddf-29d7-5469-a434-b200b6524b97",
        "name" : "滨海BRD0601_A01_17",
        "posCode" : 17
    },
    "instanceId" : "37036798-6539-5643-b68a-e86861454d6b",
    "regionId" : {
        "number" : "hs_Experience",
        "instanceId" : "74acf61f-c369-4a6a-a5ac-38e233f31483",
        "name" : "hs_Experience"
    },
    "regionHostId" : "hs_Experience_17",
    "assetId" : "tstack-asset11",
    "hostType" : "物理机",
    "posUnit" : NumberLong(1),
    "name" : "openstack-com09",
    "ctime" : ISODate("2019-04-11T11:47:57.693Z"),
    "status" : "运营中",
    "businessModuleId" : null,
    "cloudTime" : null,
    "hypervisor" : null,
    "imageId" : null,
    "networkId" : [
        ""
    ],
    "subnetId" : [
        ""
    ]
}

MongoDB   $type 操作符

$type操作符是基于BSON类型来检索集合中匹配的数据类型,并返回结果。

MongoDB 中可以使用的类型如下表所示:

如果想获取 "t_inst__Server" 集合中 title 为 String 的数据,你可以使用以下命令:

db.t_inst__Server.find({"title" : {$type : 2}})  db.t_inst__Server.find({"title" : {$type : 'string'}})、

输出结果:

MongoDB Limit() 方法

如果你需要在MongoDB中读取指定数量的数据记录,可以使用MongoDB的Limit方法,limit()方法接受一个数字参数,该参数指定从MongoDB中读取的记录条数。

语法

limit()方法基本语法如下所示:

>db.COLLECTION_NAME.find().limit(NUMBER)

以下实例为显示查询文档中的两条记录:

猜你喜欢

转载自www.cnblogs.com/lsw-blogs/p/10949956.html
今日推荐