Starting in MongoDB 5.0, the wildcardProjection option for wildcard indexes is included in the index signature. This means that you can create multiple wildcard indexes with the same key pattern as long as the wildcardProjection options do not contain the same fields.
投影签名显示
从 MongoDB 6.3、6.0.5 和 5.0.16 开始,wildcardProjection 字段以其提交的形式存储索引投影。早期版本的服务器可能已将投影以标准化形式存储。
服务器以相同的方式使用索引,但您可能会注意到listIndexes和db.collection.getIndexes()命令的输出有所不同。
例子
考虑books集合上的以下通配符索引:
db.books.createIndex( { "$**": 1 }, { wildcardProjection: { "author.name": 1, "author.website": 1 }, name: "authorWildcard" } )
索引键模式为"$**" 。 如果指定不同的wildcardProjection ,则可以使用相同的键模式创建另一个通配符索引。 示例:
db.books.createIndex( { "$**": 1 }, { wildcardProjection: { "publisher.name": 1 }, name: "publisherWildcard" } )
要查看创建的索引,请运行getIndexes()方法:
db.books.getIndexes()
输出:
[ { v: 2, key: { _id: 1 }, name: '_id_' }, { v: 2, key: { '$**': 1 }, name: 'authorWildcard', wildcardProjection: { author: { website: true, name: true }, _id: false } }, { v: 2, key: { '$**': 1 }, name: 'publisherWildcard', wildcardProjection: { publisher: { name: true }, _id: false } } ]