假设我有一个集合:
db.person.insert(
[
{
_id: 3,
name: { first: 'Grace', last: 'Hopper' },
title: 'Rear Admiral',
birth: new Date('Dec 09, 1906'),
death: new Date('Jan 01, 1992'),
contribs: [ 'UNIVAC', 'compiler', 'FLOW-MATIC', 'COBOL' ],
awards: [
{
award: 'Computer Sciences Man of the Year',
year: 1999,
by: 'Data Processing Management Association'
},
{
award: 'Distinguished Fellow',
year: 1973,
by: ' British Computer Society'
},
{
award: 'W. W. McDowell Award',
year: 1976,
by: 'IEEE Computer Society'
},
{
award: 'National Medal of Technology',
year: 1991,
by: 'United States'
}
]
},
{
_id: 4,
name: { first: 'Kristen', last: 'Nygaard' },
birth: new Date('Aug 27, 1926'),
death: new Date('Aug 10, 2002'),
contribs: [ 'OOP', 'Simula' ],
awards: [
{
award: 'Rosing Prize',
year: 1999,
by: 'Norwegian Data Association'
},
{
award: 'Turing Award',
year: 2001,
by: 'ACM'
},
{
award: 'IEEE John von Neumann Medal',
year: 2001,
by: 'IEEE'
}
]
},
]
)
问题:如何获取1999年获奖的人名?
SQL Query would be : Select first , last from person where year = 1999;
注意:是的,这是一个愚蠢的问题。但我仍然试图了解NoSQL的工作原理。我试图从关系数据库复制一些选择查询。
答案 0 :(得分:4)
mongoDB文档有一个关于SQL to MongoDB mapping的部分。但基本上你想要:
db.person.find({"awards.year":1999},{"name.first":1, "name.last":1, _id:0})
答案 1 :(得分:1)
永远不要介意我设法做到了
db.bios.find( {awards : {$elemMatch: {year:1991}}} , {'name': 1} )