如何从搜索结果文档中排除查询中不存在的字段? [英] How to exclude from search results documents with fields which are not present in query?

查看:40
本文介绍了如何从搜索结果文档中排除查询中不存在的字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文件:

  1. {p1:"a",p2:"b"}
  2. {p1:"a",p2:"b",p3:"c"}

我应该如何处理查询:{p1:"a",p2:"b"}仅查找第一个文档?因此,我只想查找具有指定字段的文档.如果文档中的字段比查询更多,则不应在搜索结果中显示该字段.

What I should to do with query: { p1:"a", p2:"b" } to find only first document? So I want find only documents with fields what I specified. If document has more fields (than query) it should not be presented in search results.

推荐答案

使用这种结构并不容易,您还需要一些其他可索引的线索来指示要离开和离开的地方.

It's not easy to do with that structure, you need some other indexable cue as to what to take and to leave.

如果知道不需要的字段,则可以使用$ exists, http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24存在,但是效率不高.

If you know what fields you DON'T want, you can use $exists, http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24exists but it's not very efficient.

db.col.find({p3:{$exists:false}})

您也可以这样做;

db.col.find({ p1:"a", p2:"b", p3:null }

您可以只舍弃自己的代码中不需要的字段吗?还是可以重组为嵌套组以使其更易于过滤?

Could you just throw away the fields you don't want in your own code? Or could you restructure into nested groups to make it easier to filter?

{ basicData:{p1:"a", p2:"b"}, extraData:{p3:"c",p4:"d"} }

这篇关于如何从搜索结果文档中排除查询中不存在的字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆