INTRODUCTION TO MONGODB MongoDB is an open-source document
database which provides high performance on big data. A record in
MongoDB is a document, which is similar to JSON format, called
BSON.
Slide 4
ADVANTAGE OF MONGODB JSON is native data type in many
programming languages. Embedded documents and arrays instead of
joins. Dynamic schema.
Slide 5
EMBEDDED VS NORMALIZED
Slide 6
MONGODB IS FAST AND SCALABLE Disk seeking and data
locality
INSERT DATA INTO MONGODB 1. Use Command: db.persons.insert()
for one document every time. 2. Or Create a variable: var doc =
[{},{},{}] db.persons.insert(doc) for multiple records
Slide 10
QUERY THE MONGODB Find all the person, whose age is less than
30, return the name. db.persons.find({age:{$lt:30}},{name:1,age:1,
_id:0}) { "name" : "Samantha", "age" : 21 } { "name" : "Chris",
"age" : 25 } { "name" : "Robot", "age" : 21 } { "name" : "Josh",
"age" : 29 } Query Operation Output Operation Collection name &
command
Slide 11
QUERY WITH ARRAY Find the grades of Josh
db.persons.find({name:"Josh"},{grades:1,_id:0}) { "grades" : [ 90,
89, 85, 65, 88 ] }
PLUGIN MODEL: - QUERY (1) &mongo_query[D, Q, P](Output) -
D: database and collection name: eg: test.persons - Q: query
criteria: eg: {age: {$lt: 30} - P: query result projection: eg:
{name;gender} (should be separated by ;) result(NAME,GENDER) :-
&mongo_query[">
Slide 17
PLUGIN MODEL: - QUERY(2) ARRAY If the mongo_query result is
array, you should run mongo_array plugin to traverse the data in
array. &mongo_array[A](Index, Value) - A: the input of an Array
- Output: Index: the index number of one array item Value: the
value of one array item result(NAME,GRADE) :-
&mongo_query["test.persons","{grades:{$exists:1}}","name;grades
"](NAME, GRADE). grade(NAME,I,G) :- &mongo_array[GRADE](I,G),
result(NAME,GRADE),I=0.
Slide 18
PLUGIN MODEL: - INSERT &mongo_insert[D, K1,
V1,[K2,V2]...]() - D: database and collection name: eg:
test.persons - K: The field of JSON Element: eg: name - V: The
value of JSON Element: eg: adam - The [K, V] should be paired, and
can have as many as you want. success :-
&mongo_insert["test.persons","name","adam","age","2
0","interest","CS"]().
Slide 19
EXAMPLE OF FALLING REASONING(1) %%%%%%%%%%%%%%%%%%%%%%%%%%%%% %
Part 1: Data Retrive from MongoDB %%%%%%%%%%%%%%%%%%%%%%%%%%%%% % %
The event of Falling has been detected %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%% falling_time(T) :-
&mongo_query["smart.example", "{'smart_ontology:isValue.0':
'fall'}", "smart_ontology:isTime.0"](T). thirty_minutes_before(T1)
:- falling_time(T), T1 = T-1800. hour_before(T2) :-
falling_time(T), T2 = T-3600. one_minute_before(T3) :-
falling_time(T), T3 = T - 60.
Slide 20
EXAMPLE OF FALLING REASONING(2) %%%%%%%%%%%%%%%%%%%%%%%%%%%%% %
Get the movement data from smartphone sensor in the range % of 30
minutes before the falling down happened.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%% movement_array(S,A) :-
&mongo_query["smart.example",
"{'smart_ontology:fromDevice.0.@id':'smart_ontology:phone1',
'smart_ontology:startTime.0': {$lt: '$INPUT'},
'smart_ontology:endTime.0': {$gt: '$INPUT'}}",
"smart_ontology:isValue;smart_ontology:startTime.0", T,T](A,S),
falling_time(T).
EXAMPLE OF FALLING REASONING(4) %%%%%%%%%%%%%%%%%%%%%%%%%%%%% %
% Get the "insulin taken" and "eating" event data from % camera
sensor in one hour before the falling down happened. %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%% take_insulin_time(T) :-
&mongo_query["smart.example",
"{'smart_ontology:[email protected]':'smart_ontology:Camera',
'smart_ontology:isValue.0':'take_insulin',
'smart_ontology:isTime.0': {$lt: '$INPUT'},
'smart_ontology:isTime.0': {$gt: '$INPUT'}}}",
"smart_ontology:isTime.0", T1, T2](T), falling_time(T1),
hour_before(T2).
Slide 23 T1.">
EXAMPLE OF FALLING REASONING(5) eat_time(T) :-
&mongo_query["smart.example",
"{'smart_ontology:[email protected]':'smart_ontology:Camera',
'smart_ontology:isValue.0':'eat','smart_ontology:isTime.0': {$lt:
'$INPUT'}, 'smart_ontology:isTime.0': {$gt: '$INPUT'}}}",
"smart_ontology:isTime.0", T1, T2](T), falling_time(T1),
hour_before(T2). eat_after_insulin_taken :- take_insulin_time(T1),
eat_time(T), T > T1.
Slide 24
EXAMPLE OF FALLING REASONING(6) %%%%%%%%%%%%%%%%%%%%%%%%%%%%% %
% Glucose testing was given every hour %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%% glucose(T,V) :-
&mongo_query["smart.example",
"{'smart_ontology:fromDevice.0.@id':'smart_ontology:glucose1','smart_ontology:isTime.0':
{$lt: '$INPUT'}, 'smart_ontology:isTime.0': {$gt: '$INPUT'}}}",
"smart_ontology:isTime.0;smart_ontology:isValue.0", T1, T2](T,V),
falling_time(T1), hour_before(T2).
Slide 25
EXAMPLE OF FALLING REASONING(7) %%%%%%%%%%%%%%%%%%%%%%%%%%%%% %
Get the heart rate 1 minute before the falling down event
%%%%%%%%%%%%%%%%%%%%%%%%%%%%% heart_rate_array(A) :-
&mongo_query["smart.example",
"{'smart_ontology:fromDevice.0.@id':'smart_ontology:heartrate1',
'smart_ontology:startTime.0': {$lt: '$INPUT'},
'smart_ontology:endTime.0': {$gt: '$INPUT'}}",
"smart_ontology:isValue", T,T](A), one_minute_before(T).
heart_rate(I,V) :- &mongo_array[A](I,V), heart_rate_array(A), V
!= "NULL". not_newest_heart_rate(I,V) :- heart_rate(I,V),
heart_rate(I1,V1), I < I1. newest_heart_rate(V) :-
heart_rate(I,V), not not_newest_heart_rate(I,V).
Slide 26 50.">
EXAMPLE OF FALLING REASONING(8) %%%%%%%%%%%%%%%%%%%%%%%%%%%%% %
Part 2: Reasoning Task %%%%%%%%%%%%%%%%%%%%%%%%%%%%% % If the
activities of run is dominant in the previous 30 % minutes, the
person has been activiated intensively. That % might be one reason
of falling down. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
intensive_activity_possibility(Y1) :-
&count[movement_in_30_minutes](X1),
&count[movement_in_30_minutes,mask,"run"](X2), Y = X2*100, Y1 =
Y/X1. reason_of_falling("intensive_activity") :-
intensive_activity_possibility(Y), Y > 50.
Slide 27
EXAMPLE OF FALLING REASONING(9) %%%%%%%%%%%%%%%%%%%%%%%%%%%%% %
% If the person has low glucose level and not eat after the %
insulin taking, or the use has two insulin taking in the % previous
hour, the reason of falling might be "insulin shock" %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%% reason_of_falling("insulin_shock") :-
glucose(T,V), V< 50, not eat_after_insulin_taken.
reason_of_falling("insulin_shock") :- glucose(T,V), V = 2.
Slide 28
EXAMPLE OF FALLING REASONING(10) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% If the person has a heart attack that the heart almost stop %
beating, the falling down reason would be "heart attack
%%%%%%%%%%%%%%%%%%%%%%%%%%%%% reason_of_falling("heart_attack") :-
newest_heart_rate(V), V < 20. %%%%%%%%%%%%%%%%%%%%%%%%%%%%% % %
Insert the reasoning result back to the database %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%% insert_success:-
&mongo_insert["smart.reasoning", "reason_of_falling", X](),
reason_of_falling(X).
Slide 29
HOMEWORK 1. Insert yourself into the MongoDB in test.persons.
You can put your name, your gender, and anything else you like. 2.
Check the data in the MongoDB in basis.sensor, and run a dlvhex
mongo-query to return the step data in a day, which is not 0. The
output should look like this:
{records("steps",1402619100,76),records("steps",1402619040,41),records("steps",1402618980,4),records("steps",14026189
20,24) }