简体   繁体   English

仅按 mongodb 中的值匹配并返回文档的 id

[英]Match only by values in mongodb and return id of the document

I have inserted some json data into mongodb and I wanted to perform a simple search by matching only the values irrespective of the keys (Since keys are different for different documents) and wanted to return the id of the document.我已经在 mongodb 中插入了一些 json 数据,我想通过只匹配值而不考虑键(因为不同文档的键不同)来执行简单的搜索,并且想要返回文档的 id。 I don't know how to compare only by values in mongodb.我不知道如何仅通过 mongodb 中的值进行比较。

Example: Suppose if am searching for word "Knowledge" it should return all the ids of the document which contain the word "Knowledge" irrespective of its key value.示例:假设如果我搜索单词“Knowledge”,它应该返回包含单词“Knowledge”的文档的所有 id,而不考虑其键值。

您需要使用通配符文本索引

db.collection.createIndex( { "$**": "text" } )

If there is a static superset of fieldnames, you may find text indexes and the $text query operator useful for word-based searches.如果存在字段名的静态超集,您可能会发现 文本索引$text查询运算符对基于单词的搜索很有用。

Create the text index on every potential field, and those contained in each document will be included.在每个潜在字段上创建文本索引,每个文档中包含的那些将被包括在内。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM