MongoDB Chapter1
MongoDB Chapter1
Donny Winston
Instructor
JavaScript Object Notation (JSON)
Objects {} Arrays []
{ "instructor_1",
'instructor': true ]
},
Values:
· strings str
· null None
--
Using .
Donny Winston
Instructor
An example "laureates" document
{'_id': ObjectId('5b9ac94ff35b63cf5231ccb1'),
'born': '1845-03-27',
'bornCity': 'Lennep (now Remscheid)',
'bornCountry': 'Prussia (now Germany)',
'bornCountryCode': 'DE',
'died': '1923-02-10',
'diedCity': 'Munich',
'diedCountry': 'Germany',
'diedCountryCode': 'DE',
'firstname': 'Wilhelm Conrad',
'gender': 'male',
'id': '1',
'prizes': [{'affiliations': [{'city': 'Munich',
'country': 'Germany',
'name': 'Munich University'}],
'category': 'physics',
'motivation': '"in recognition of the extraordinary services '
'he has rendered by the discovery of the '
'remarkable rays subsequently named after him"',
'share': '1',
'year': '1901'}],
'surname': 'Röntgen'}
filter_doc = {
'born': '1845-03-27',
'diedCountry': 'Germany',
'gender': 'male',
'surname': 'Röntgen'
}
db.laureates.count_documents(filter_doc)
48
db.laureates.count_documents({'diedCountry': 'France'})
50
db.laureates.count_documents({'bornCity': 'Warsaw'})
db.laureates.find_one(filter_doc)
{'_id': ObjectId('5bc56154f35b634065ba1be9'),
'born': '1867-11-07',
'bornCity': 'Warsaw',
'bornCountry': 'Russian Empire (now Poland)',
'bornCountryCode': 'PL',
'died': '1934-07-04',
'diedCity': 'Sallanches',
'diedCountry': 'France',
'diedCountryCode': 'FR',
'firstname': 'Marie',
...
db.laureates.count_documents({
{
'diedCountry': {
# Match a single value exactly:
'$in': ['France', 'USA']}})
'field_name1': value1,
872
Donny Winston
Instructor
A functional density
db.laureates.find_one({ db.laureates.count_documents({
"firstname": "Walter", "prizes.affiliations.name": (
"surname": "Kohn"}) "University of California")})
{'born': '1923-03-09', 34
'bornCity': 'Vienna',
'bornCountry': 'Austria',
'firstname': 'Walter', db.laureates.count_documents({
'prizes': [ "prizes.affiliations.city": (
{'affiliations': [ "Berkeley, CA")})
{'city': 'Santa Barbara, CA',
'country': 'USA',
'name': ('University of ' 19
'California')
}],
'category': 'chemistry',
'motivation': (
'"for his development of the '
'density-functional theory"'),
'share': '2',
'year': '1998'
}],
'surname': 'Kohn',
...} # showing partial document
{'_id': ObjectId('5b9ec791f35b63093c3d98b7'), 31
'born': '1932-08-17',
'died': '2018-08-11',
'diedCity': 'London',
'diedCountry': 'United Kingdom',
'diedCountryCode': 'GB',
'firstname': 'Sir Vidiadhar Surajprasad',
'gender': 'male',
'id': '747',
'prizes': [{'affiliations': [[]],
'category': 'literature',
'motivation': ('"for having united perceptive narrative and '
'incorruptible scrutiny in works that compel us '
'to see the presence of suppressed histories"'),
'share': '1',
'year': '2001'}],
'surname': 'Naipaul'}
922
922
922