mongodb - understanding read operations

Suppose I have a collection:

   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'
                 }
               ]
     },

   ]
)

Question:how to fetch person's name who have received an award on 1999?

SQL Query would be : Select first , last from person where year = 1999;

NOTE: Yes, It's a stupid question. But I am still trying to understand how NoSQL works. I am trying to replicate some select query from relational database.

Upvotes: 0

Views: 75

Answers (2)

Never-mind I managed to do it

db.bios.find( {awards : {$elemMatch: {year:1991}}} , {'name': 1} )

Upvotes: 1

jimoleary
jimoleary

Reputation: 1645

The mongoDB documentation has a section on SQL to MongoDB mapping. But essentially you want :

db.person.find({"awards.year":1999},{"name.first":1, "name.last":1, _id:0})

Upvotes: 4

Related Questions