文件如下:
{
name:"james",
files:[
{name:"file1.txt", content:"..."},
{name:"file2.txt", content:"..."},
{name:"file3.txt", content:"..."}
],
status:4
}想要进行查询并返回:
filesfiles只包含file2.txt信息,coze content很大,只对files2.txt感兴趣我知道在查找查询中使用$elemMatch,如下所示:
db.collection.find(
{'files':{$elemMatch:{name:'file2.txt'}} },
{name:1, status:1, 'files.$':1 }
)有没有办法避免列出投影中的所有字段并得到所有的键?
要求:要求所有字段,但不列出键(不要列出name:1, status:1)
发布于 2015-10-14 09:22:38
这就是您所要求的,返回所有字段,但是对于files字段,只返回"files2.txt“值吗?
db.collection.find({ 'files':{$elemMatch:{name:'file2.txt'}} }, { 'files.$':1, name:1, status:1 } )https://stackoverflow.com/questions/33120655
复制相似问题