-
Notifications
You must be signed in to change notification settings - Fork 0
/
Model-I-Queries.js
66 lines (60 loc) · 2.25 KB
/
Model-I-Queries.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//
// Query I: Query to find a string in the "Text" field in "tweets" collection
//
var start = new Date().getTime();
db.tweets.find({$text:{$search:"good"}},{ID:1,UserID:1,_id:0})
var end = new Date().getTime();
var time = end - start;
print('Execution time to find an existing text: ' + time+' msec');
var start = new Date().getTime();
db.tweets.find({$text:{$search:"adfafda"}},{ID:1,UserID:1,_id:0})
var end = new Date().getTime();
var time = end - start;
print('Execution time to find a non-existent text: ' + time+' msec');
//
// Query II:
// Return cumulated retweet counts of all tweets, each of which has at
// least one hashtag.
var start = new Date().getTime();
db.tweets.aggregate([{$match:{Hashtags:{$ne:""}}},{$project:{_id:0,RetCount:1}},{$group:{_id:"",cumulatedRetweetCount:{$sum:"$RetCount"}}},{$project:{cumulatedRetweetCount:1,_id:0}}])
var end = new Date().getTime();
var time = end - start;
if(time>1000){
print('Execution time to find cumulated retweet count: ' + time/1000 +' secs');
}else{
print('Execution time to find cumulated retweet count: '+ time +' msec');
}
//
// Query III:
// Query to find user with highest number of followers and list the
// names of the followers
//
var start = new Date().getTime();
var userID=db.users.find({},{user_id:1,_id:0}).sort({follower_count:-1}).limit(1);
while (userID.hasNext()){
var follUserID = db.network.find({userid1:userID.next().user_id},{userid2:1,_id:0})
while (follUserID.hasNext()){
db.users.find({user_id : follUserID.next().userid2},{user_name :1,_id:0})
}
}
var end = new Date().getTime();
var time = end - start;
if(time>1000){
print('Execution time to find follower names: ' + time/1000 +' secs');
}else{
print('Execution time to find follower names: '+ time +' msec');
}
//
// Query IV:
// Query to insert a follower for a user and increment the follower count in users collection
// names of the followers
//
var start = new Date().getTime();
db.network.update({userid1:7000002},{$set:{userid2:7000000}},{upsert:true})
db.users.update({user_id:70000002},{$inc:{follower_count:1}})
var time = end - start;
if(time>1000){
print('Execution time to insert a follower: ' + time/1000 +' secs');
}else{
print('Execution time to insert a follower: '+ time +' msec');
}