0

假设我有一个集合:

   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 的工作原理。我正在尝试从关系数据库中复制一些选择查询。

4

2 回答 2

4

mongoDB 文档有一个关于SQL 到 MongoDB 映射的部分。但本质上你想要:

db.person.find({"awards.year":1999},{"name.first":1, "name.last":1, _id:0})
于 2013-06-07T10:50:09.233 回答
1

没关系,我设法做到了

db.bios.find( {awards : {$elemMatch: {year:1991}}} , {'name': 1} )
于 2013-06-07T10:57:46.983 回答