forked from datadolphyn/R
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MongoFns.R
212 lines (182 loc) · 5.13 KB
/
MongoFns.R
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# Interact with MongoDB
# Author: Jitender Aswani, Co-Founder @datadolph.in
# Date: 3/15/2013
# Copyright (c) 2011, under the Creative Commons Attribution-NonCommercial 3.0 Unported (CC BY-NC 3.0) License
# For more information see: https://creativecommons.org/licenses/by-nc/3.0/
# Packages Used: rmongodb
# All rights reserved.
require("rmongodb")
mongo.db <- list(user="", pass="", name="ddfin_dev", system.pads="ddfin_dev.system_pads", host="localhost")
#
#initiate a connection to admin database
#
getDefaultMongoDBCon <- function() {
mongo <- mongo.create()
if (!mongo.is.connected(mongo)) {
print("Unable to connect. Error code:")
print(mongo.get.err(mongo))
} else
return(mongo)
}
#
#initiate a connection to a db
# This will create a db if it doesn't exist
#
getMongoDBCon <- function(db.name) {
mongo <- mongo.create(db=db.name)
if (!mongo.is.connected(mongo)) {
cat("Unable to connect. Error code:", "\n")
cat(mongo.get.err(mongo), "\n")
} else
return(mongo)
}
#
#disconnect
#
disconnectMongoDB <- function(mongo) {
#Close Connection
if (mongo.is.connected(mongo)) {
mongo.destroy(mongo)
}
}
#
#list all databases in Mongo
#
listAllDBs <- function() {
#List dbs
mongo <- getDefaultMongoDBCon()
on.exit(disconnectMongoDB(mongo)) # On function exit, close connection
cat(mongo.get.databases(mongo), "\n")
}
#
# drop a db
#
dropDB <- function(db.name) {
mongo <- getDefaultMongoDBCon()
on.exit(disconnectMongoDB(mongo)) # On function exit, close connection
cat(mongo.drop.database(mongo, db.name))
}
#
#List collections for a given db
#
listAllCollections <- function(db.name) {
mongo <- getDefaultMongoDBCon()
on.exit(disconnectMongoDB(mongo)) # On function exit, close connection
cat(mongo.get.database.collections(mongo, db.name), "\n")
}
#listAllCollections(mongo.db$name)
#
# drop a collection
#
dropCollection <- function(db.collection){
mongo <- getDefaultMongoDBCon()
on.exit(disconnectMongoDB(mongo))
cat(mongo.drop(mongo, db.collection), "\n")
}
#dropCollection(mongo.db$system.pads)
#
# empty a collection
#
emptyCollection <- function(db.collection){
mongo <- getDefaultMongoDBCon()
on.exit(disconnectMongoDB(mongo))
cat(mongo.remove(mongo, db.collection), "\n")
}
#emptyCollection(mongo.db$system.pads)
#
# insert systemPads
#
insertPadDoc <- function(doc){
if(!is.null(mongo)){
status <- mongo.insert(mongo, mongo.db$system.pads, doc)
if(status){
if(verbose) cat("pad successfully inserted", "\n")
} else {
err <- mongo.get.last.err(mongo, mongo.db$name)
cat(mongo.get.server.err(mongo), "\n")
cat(mongo.get.server.err.string(mongo), "\n")
}
} else {
cat("Connection is null", "\n")
}
mongo.bson.destroy(doc)
}
#
# update systemPads
#
updatePadDoc <- function(pad.id, doc){
if(!is.null(mongo)){
buf <- mongo.bson.buffer.create()
mongo.bson.buffer.append(buf, "_id", pad.id)
criteria <- mongo.bson.from.buffer(buf)
status <- mongo.update(mongo, mongo.db$system.pads, criteria, doc)
if(status){
if(verbose) cat("pad successfully updated", "\n")
} else {
err <- mongo.get.last.err(mongo, mongo.db$name)
cat(mongo.get.server.err(mongo), "\n")
cat(mongo.get.server.err.string(mongo), "\n")
}
} else {
cat("Connection is null", "\n")
}
mongo.bson.destroy(doc)
}
#
#insert a pad into system_pads
# connection object is supplied
#
insertPadToMongo <- function(pad.id, pad){
# Insert document to collection 'system_pads'
doc <- mongo.bson.from.list(list(
"_id"=pad.id,
pad=pad))
if(verbose) cat("calling insert for pad ", pad.id, "\n")
insertPadDoc(doc)
}
#
# get a single system pad
#
getSingleSystemPad <- function(padId) {
mongo <- getDefaultMongoDBCon()
on.exit(disconnectMongoDB(mongo))
buf <- mongo.bson.buffer.create()
mongo.bson.buffer.append(buf, "_id", padId)
query <- mongo.bson.from.buffer(buf)
# Find the first 100 records in collection system_pads
cursor <- mongo.find(mongo, mongo.db$system.pads, query)
# Step though the matching records and display them
while (mongo.cursor.next(cursor))
l<- mongo.bson.to.list(mongo.cursor.value(cursor))
#assign("l", l, envir=.GlobalEnv)
#l$pad$default$type = "timeseries"
#updatePadDoc(l$'_id', mongo.bson.from.list(l))
mongo.cursor.destroy(cursor)
}
#getSingleSystemPad("pad136971841842064")
#
#get pads from system_pads
#
getSystemPads <- function() {
mongo <- getDefaultMongoDBCon()
on.exit(disconnectMongoDB(mongo))
# Find the first 100 records in collection system_pads
cursor <- mongo.find(mongo, mongo.db$system.pads, limit=100L)
# Step though the matching records and display them
while (mongo.cursor.next(cursor))
print(mongo.cursor.value(cursor))
mongo.cursor.destroy(cursor)
}
#getSystemPads()
#
#get pads count from system_pads
#
getSystemPadsCount <- function(){
mongo <- getDefaultMongoDBCon()
on.exit(disconnectMongoDB(mongo))
# Find the first 100 records
# in collection people of database test where age == 18
pads.count <- mongo.count(mongo, mongo.db$system.pads)
cat("total pads: ", pads.count, "\n")
}
#getSystemPadsCount()