I am a Quantitative Analyst/Developer and Data Scientist with backgroud of Finance, Education, and IT industry. This site contains some exercises, projects, and studies that I have worked on. If you have any questions, feel free to contact me at ih138 at columbia dot edu.
sudo apt-get install -y mongodb-org
sudo service mongod start
# Listen to local interface only. Comment out to listen on all interfaces. #bind_ip = 127.0.0.1
$ sudo service mongod stop mongod stop/waiting $ sudo service mongod start mongod start/running, process 14087
https://docs.mongodb.org/getting-started/shell/import-data/
https://raw.githubusercontent.com/mongodb/docs-assets/primer-dataset/dataset.json
save to a file named primer-dataset.json.
Now, Import data into the collection from bash shell (not mongo shell)
db name: test
collection name: restaurants
if there is a same collection, drop it first
$ mongoimport --db test --collection restaurants --drop --file primer-dataset.json
$ mongoNow, in the mongo shell
use test
db.restaurants.insert(
{
"address" : {
"street" : "2 Avenue",
"zipcode" : "10075",
"building" : "1480",
"coord" : [ -73.9557413, 40.7720266 ],
},
"borough" : "Manhattan",
"cuisine" : "Italian",
"grades" : [
{
"date" : ISODate("2014-10-01T00:00:00Z"),
"grade" : "A",
"score" : 11
},
{
"date" : ISODate("2014-01-16T00:00:00Z"),
"grade" : "B",
"score" : 17
}
],
"name" : "Vella",
"restaurant_id" : "41704620"
}
)
db.restaurants.find( { "borough": "Manhattan" } )
db.restaurants.find( { "address.zipcode": "10075" } )
db.restaurants.find( { "grades.grade": "B" } )
db.restaurants.find( { "grades.score": { $gt: 30 } } )
db.restaurants.find( { "grades.score": { $lt: 10 } } )
db.restaurants.find( { "cuisine": "Italian", "address.zipcode": "10075" } )
db.restaurants.find(
{ $or: [ { "cuisine": "Italian" }, { "address.zipcode": "10075" } ] }
)
db.restaurants.find().sort( { "borough": 1, "address.zipcode": 1 } )
db.collection.find() method from MongoDB shell, returns a cursor to the matching documents.
var myCursor = db.inventory.find();
var myFirstDocument = myCursor.hasNext() ? myCursor.next() : null;
myCursor.objsLeftInBatch();
var myCursor = db.inventory.find( { type: 'food' } );
while (myCursor.hasNext()) {
print(tojson(myCursor.next()));
}
var myCursor = db.inventory.find( { type: 'food' } );
while (myCursor.hasNext()) {
printjson(myCursor.next());
}
The toArray() method loads into RAM all documents returned.
var myCursor = db.inventory.find( { type: 'food' } );
myCursor.forEach(printjson);
var myCursor = db.inventory.find( { type: 'food' } );
var documentArray = myCursor.toArray();
var myDocument = documentArray[3];
var myCursor = db.inventory.find( { type: 'food' } );
var myDocument = myCursor[3];
# The last line is same to the below:
myCursor.toArray()[3];
# first document matched. $currentDate operator is useful.
db.restaurants.update(
{ "name" : "Juni" },
{
$set: { "cuisine": "American (New)" },
$currentDate: { "lastModified": true }
}
)
db.restaurants.update(
{ "restaurant_id" : "41156888" },
{ $set: { "address.street": "East 31st Street" } }
)
# Multiple documents. This case will change ALL document matched.
db.restaurants.update(
{ "address.zipcode": "10016", cuisine: "Other" },
{
$set: { cuisine: "Category To Be Determined" },
$currentDate: { "lastModified": true }
},
{ multi: true}
)
# Replace a document
db.restaurants.update(
{ "restaurant_id" : "41704620" },
{
"name" : "Vella 2",
"address" : {
"coord" : [ -73.9557413, 40.7720266 ],
"building" : "1480",
"street" : "2 Avenue",
"zipcode" : "10075"
}
}
)
# remove all document matched by default
db.restaurants.remove( { "borough": "Manhattan" } )
# remove only one document matched.
db.restaurants.remove( { "borough": "Queens" }, { justOne: true } )
# remove all docs
db.restaurants.remove( { } )
# drop a collection
db.restaurants.drop()
# Using group stage and $sum accumulator
db.restaurants.aggregate(
[
{ $group: { "_id": "$borough", "count": { $sum: 1 } } }
]
);
# The result will be:
{ "_id" : "Staten Island", "count" : 969 }
{ "_id" : "Brooklyn", "count" : 6086 }
{ "_id" : "Manhattan", "count" : 10259 }
{ "_id" : "Queens", "count" : 5656 }
{ "_id" : "Bronx", "count" : 2338 }
{ "_id" : "Missing", "count" : 51 }
# Filter and Group docs
db.restaurants.aggregate(
[
{ $match: { "borough": "Queens", "cuisine": "Brazilian" } },
{ $group: { "_id": "$address.zipcode" , "count": { $sum: 1 } } }
]
);
# The result will be:
{ "_id" : "11368", "count" : 1 }
{ "_id" : "11106", "count" : 3 }
{ "_id" : "11377", "count" : 1 }
{ "_id" : "11103", "count" : 1 }
{ "_id" : "11101", "count" : 2 }
# Index on "cuisine" field.
db.restaurants.createIndex( { "cuisine": 1 } )
# Create a compound index.
db.restaurants.createIndex( { "cuisine": 1, "address.zipcode": -1 } )
Download php.ini and put it in "~/www", then check phpinfo.() again, Loaded configuration file
Check mongeDB setting
In /etc/mongod.conf
bind_ip= 127.0.0.1 , mongoDB's private ip , mongoDB's public ip #caution: no space between commaif you got this error,
sudo service mongod stop sudo service mongod start
tail -f /var/log/mongodb/mongod.logIt should shows
I NETWORK [initandlisten] waiting for connections on port 27017
use nota
db.mstudents.insert(
{
"name": {
"first":"Finst",
"last" :"goodstud",
},
"sql_id": 143294,
"username":"Finst"
}
)
db.mstudents.insert(
{
"name": {
"first":"Kimmer",
"last" :"Goods",
},
"sql_id": 143295,
"username":"Kimmer"
}
)
#### Multiple documents --> use array.
db.mstudents.insert(
[
{
"name": {
"first":"Finst",
"last" :"goodstud",
},
"sql_id": 143294,
"username":"Finst"
},
{
"name": {
"first":"Kimmer",
"last" :"Goods",
},
"sql_id": 143295,
"username":"Kimmer"
}
]
)
db.mstudents.find({"sql_id": 143295 })
#### Add new fields.
db.mstudents.update(
{"sql_id": 143295},
{
$set: { "temp": "temp_value" },
$currentDate: { "lastModified": true }
}
)
#### Remove fields one by one.
db.mstudents.update(
{"sql_id": 143295},
{
$unset: { "temp": "" }
}
)
db.mstudents.update(
{"sql_id": 143295},
{
$unset: { "lastModified":""}
}
)