在 MongoDB 中,您可以拥有包含 2dsphere(版本 2)字段的非稀疏复合索引吗?

In MongoDB, can you have non-sparse compound indexes containing 2dsphere (version 2) fields?

假设我有以下形式的文档

{
  _id: String,
  quality : String,
  loc: {
    type: String,
    coodinates: [Number]
  }
}

MongoDB 文档说 2dsphere 索引默认是稀疏的,但不清楚是否可以将它们强制为非稀疏。

我可以定义一个稀疏的复合索引{quality: 1, loc: '2dsphere'}吗?也就是说,我可以定义一个索引,我可以在其中搜索具有特定 quality 的文档,即使这些文档没有 loc.

我在 MongoDB 或 Mongoose(我正在使用的 ORM)的文档中找不到任何内容。

TL;不幸的是DR NO check/reproduce 它在 Mongo Shell:

use temp
db.places.insert({loc: {type: "Point", coordinates: [ -73.88, 40.78 ] }, name: "La Guardia Airport", category : "Airport" })
db.places.insert({loc: {type: "Point", coordinates: [-73.97, 40.77 ]}, name: "Central Park", category : "Parks"})
db.places.insert({name: "test no geo", category : "test"})
db.places.createIndex( { category:1, loc : "2dsphere"}, {sparse:false, name:'catsp'})
db.places.find({category:'Parks'}).hint('catsp')[0]
>>> { "_id" : ObjectId("5536d3e9d97d8ef614b8c1f6"), "loc" : { "type" : "Point", "coordinates" : [ -73.97, 40.77 ] }, "name" : "Central Park", "category" : "Parks" }
db.places.find({category:'test'}).hint('catsp')
>>> you will get NOTHING