首页 > 代码库 > mongodb "$"的作用

mongodb "$"的作用

{
    "_id": ObjectId("58e48e32830f398e8f401a95"),
    "_class": "com.ydd.model.mongdb.WeiXinQuestion",
    "openid": "oDVO_w2juwMejm7S-UxfLaSOC0kU21",
    "createtime": NumberLong(1491373618008),
    "endtime": NumberLong(0),
    "antotal": NumberLong(2),
    "content": "asdfasdfasdfasdfasdf",
    "status": 0,
    "isshow": 1,
    "address": "",
    "sex": 0,
    "labels": "气血两虚",
    "besttotal": 0,
    "bestscore": 0,
    "clerkAnswer": [
        {
            "_id": "p0k94bZ4EHV9XsftlMlOzHS8L",
            "clerkId": 2159,
            "headerimg": "head/2159/201612/1480922352820.jpeg",
            "name": "药点点店长7",
            "content": "高难度",
            "createtime": NumberLong(1491373686025),
            "adopt": 0,
            "score": 0,
            "zan": 0,
            "ids": [
                
            ],
            "clerkAnswerScore": [
                
            ],
            "qid": "58e48e32830f398e8f401a95",
            "status": 0,
            "isReward": 0,
            "replayAnswers": [
                
            ]
        },
        {
            "_id": "GkJtDMB7uizt699qFyEhAINZ0",
            "clerkId": 2166,
            "headerimg": "head/2166/201701/1484742330434.jpeg",
            "name": "药点店员13",
            "content": "但",
            "createtime": NumberLong(1491373800122),
            "adopt": 0,
            "score": 0,
            "zan": 0,
            "ids": [
                
            ],
            "clerkAnswerScore": [
                
            ],
            "qid": "58e48e32830f398e8f401a95",
            "status": 0,
            "isReward": 0,
            "replayAnswers": [
                
            ]
        },
        {
            "_id": ObjectId("58b5b0eead349879b2871348"),
            "answerId": "L7izf6lVoyY9QyERd0m2",
            "clerkId": 4625,
            "headerimg": "/resource/2016/8/26/c160c426-a.jpeg",
            "name": "林建丽",
            "content": "合生元益生菌",
            "createtime": NumberLong(1488302318220),
            "adopt": 0,
            "score": 0,
            "zan": 0,
            "ids": [
                
            ],
            "clerkAnswerScore": [
                
            ],
            "qid": "58e48e32830f398e8f401a95",
            "status": 0,
            "isReward": 0,
            "replayAnswers": [
                
            ]
        }
    ]
}

当要只查询clerkAnswer这个数组中的某条数据时可以这样查find({"clerkAnswer._id":"GkJtDMB7uizt699qFyEhAINZ0"},{"clerkAnswer.$":{$slice:1}}) ,如果你在<查询条件>中查询的内容是array里的内容,用$就能引用前查询中匹配到的元素。所以不需要遍历就能直接取到数组中的某一个对象。如果把clerkAnswer.$改为clerkAnswer,查询到的就是三条记录中的第一条了,而不是前边指定的那条,当然如果你前边的查询条件没有具体包含到数组里的数据比如你是这样查的 find({"_id":ObjectId("58e48e32830f398e8f401a95")},{"clerkAnswer.$":{$slice:1}})这样就查不到clerkAnswer中指定的某条了

mongodb "$"的作用