在将全文搜索与textscore结合使用时,如何投影字段?

时间:2018-10-18 06:56:43

标签: mongodb mongoose mongoose-schema

这是我使用猫鼬在集合上触发的查询:

mongoose.model('UniqueXperience')
    .find({
      '$text': {
        '$search': text
      }
    }, {
      'score': {
        '$meta': 'textScore'
      }
    })
    .sort({
      'score': {
        '$meta': 'textScore'
      }
    })
    .exec()
    .then((searchResults) => {
      return searchResults;
    })
    .catch((error) => {
      console.log(`There was an error: ${error}`);
    });

因此,我使用$ text运算符进行了全文搜索。 我正在从函数调用中传递文本值。 我已经使用分数来找到分数,然后对其进行相应的排序。

现在我得到的结果是整个对象:

[
        {
            "xperienceSubCategory": [
                "5b558e1d97199d3ec4953e70"
            ],
            "meetingLocation": [],
            "images": [
                {
                    "public_id": "zkwm63tzvyh7ob4tqsvc",
                    "version": 1537776752,
                    "signature": "bc04daf3c44bae22c6066dc3a4d9f3508129e2d8",
                    "width": 768,
                    "height": 373,
                    "format": "jpg",
                    "resource_type": "image",
                    "url": "http://res.cloudinary.com/xplr-qa/image/upload/v1537776752/zkwm63tzvyh7ob4tqsvc.jpg",
                    "secure_url": "https://res.cloudinary.com/xplr-qa/image/upload/v1537776752/zkwm63tzvyh7ob4tqsvc.jpg",
                    "_id": "5ba89c71ac69b4784a4f3a1c"
                },
                {
                    "public_id": "bqowagqxz0mexif17xcl",
                    "version": 1537776752,
                    "signature": "0cfdf1b33ec9eb5a7c1b30e17f48a8f4ed173082",
                    "width": 1024,
                    "height": 683,
                    "format": "jpg",
                    "resource_type": "image",
                    "url": "http://res.cloudinary.com/xplr-qa/image/upload/v1537776752/bqowagqxz0mexif17xcl.jpg",
                    "secure_url": "https://res.cloudinary.com/xplr-qa/image/upload/v1537776752/bqowagqxz0mexif17xcl.jpg",
                    "_id": "5ba89c71ac69b4784a4f3a1b"
                },
                {
                    "public_id": "noimdnoyeqstg3hc4szh",
                    "version": 1537776752,
                    "signature": "0f63a75ac3d51fcb33f92c97a8fa34038bb8d7dd",
                    "width": 1024,
                    "height": 682,
                    "format": "jpg",
                    "resource_type": "image",
                    "url": "http://res.cloudinary.com/xplr-qa/image/upload/v1537776752/noimdnoyeqstg3hc4szh.jpg",
                    "secure_url": "https://res.cloudinary.com/xplr-qa/image/upload/v1537776752/noimdnoyeqstg3hc4szh.jpg",
                    "_id": "5ba89c71ac69b4784a4f3a1a"
                }
            ],
            "_id": "5b4de6e292c60e0739e45159",
            "updatedBy": "5b433dbf6a93e56a21f610eb",
            "updatedAt": "2018-10-10T10:56:27.219Z",
            "createdBy": "5b433dbf6a93e56a21f610eb",
            "createdAt": "2018-07-17T12:53:54.223Z",
            "xpert": "5b518101752982066a0dcc08",
            "title": "Amusement Ride",
            "organisationFlag": false,
            "description": "Ride all in one day",
            "__v": 5,
            "source": "",
            "price": 800,
            "coordinates": [
                72.955162,
                19.172098
            ],
            "primaryLocation": "5bacb0192e0b2751f0f7f057",
            "purchaseCount": 15,
            "score": 2.1666666666666665
        },
        {
            "xperienceSubCategory": [
                "5b4de8199f07bf0b303b8c5f"
            ],
            "meetingLocation": [],
            "images": [
                {
                    "public_id": "dotttg3xm0vvqygr4n1w",
                    "version": 1537776979,
                    "signature": "4fb562aa4115941e78f773dd11ed6be37a337acd",
                    "width": 1280,
                    "height": 640,
                    "format": "jpg",
                    "resource_type": "image",
                    "url": "http://res.cloudinary.com/xplr-qa/image/upload/v1537776979/dotttg3xm0vvqygr4n1w.jpg",
                    "secure_url": "https://res.cloudinary.com/xplr-qa/image/upload/v1537776979/dotttg3xm0vvqygr4n1w.jpg",
                    "_id": "5ba89d55ac69b4784a4f3a1f"
                },
                {
                    "public_id": "cfdepst0r2wz39rowvzd",
                    "version": 1537776980,
                    "signature": "e770b7354c01ffce61c2e3cb83c79456dacc5438",
                    "width": 2100,
                    "height": 1526,
                    "format": "jpg",
                    "resource_type": "image",
                    "url": "http://res.cloudinary.com/xplr-qa/image/upload/v1537776980/cfdepst0r2wz39rowvzd.jpg",
                    "secure_url": "https://res.cloudinary.com/xplr-qa/image/upload/v1537776980/cfdepst0r2wz39rowvzd.jpg",
                    "_id": "5ba89d55ac69b4784a4f3a1e"
                },
                {
                    "public_id": "lqfk6twitfefrjq1h2jq",
                    "version": 1537776979,
                    "signature": "ad1e0079a9419bde0047228446be17218117cf2e",
                    "width": 2500,
                    "height": 1666,
                    "format": "jpg",
                    "resource_type": "image",
                    "url": "http://res.cloudinary.com/xplr-qa/image/upload/v1537776979/lqfk6twitfefrjq1h2jq.jpg",
                    "secure_url": "https://res.cloudinary.com/xplr-qa/image/upload/v1537776979/lqfk6twitfefrjq1h2jq.jpg",
                    "_id": "5ba89d55ac69b4784a4f3a1d"
                }
            ],
            "_id": "5b558b5997199d3ec4953e6b",
            "updatedBy": "5b433dbf6a93e56a21f610eb",
            "updatedAt": "2018-10-10T10:56:36.952Z",
            "createdBy": "5b433dbf6a93e56a21f610eb",
            "createdAt": "2018-07-23T08:01:29.125Z",
            "title": "Horse riding at EIRS",
            "organisationFlag": false,
            "xpert": "5b51b054054a962cc6f914db",
            "description": "Beautiful rugged landscape makes Ladakh stand apart in the country. There is indeed so much that needs to be explored here and little did we know that amongst the best ways to explore this place is riding a horse. In the remote places like Zanskar Valley, where paved roads are rare and motor biking is not every ones cup of tea, horses make an incredible means of travel. One can access to Zangla, Penzela Pass, Panikhar Fort and Zongkhul, Stongdey and Gelugpa monasteries on a horseback.",
            "__v": 4,
            "source": "",
            "price": 400,
            "coordinates": [
                72.955162,
                19.172098
            ],
            "purchaseCount": 10,
            "score": 1.842767295597484
        }
    ]

现在我要投影一些字段,例如标题,描述,图像和价格。

我正在尝试使用以下查询进行投影:

db.getCollection('uniquexperiences').find({
  '$text': {
    '$search': 'Ride'
  }
}, {
  'score': {
    '$meta': 'textScore'
  }
}, {
    'title': 1,
    'description': 1,
    'images': 1,
    'score': 1
}).sort({
  'score': {
    '$meta': 'textScore'
  }
})

但这没有效果。

任何人都可以提出一种投影方法吗?

0 个答案:

没有答案