2013-04-23 121 views
3

我有一個Twitter數據CSV文件,我已經閱讀R使用read.table,然後我對這些數據執行了一些算法,並得到了表格數據的輸出。我想將此表格數據插入mongodb。任何人都可以幫忙嗎?從R到Mongodb插入data.frame

表格數據看起來是這樣的:

pqr 
      V2       V1  V3   V4  polarity emotion 
    1 342521635332_318304224958367 FB 2013-03-15 6:43 PM positive <NA> 
    2 342521635332_325506614238815 FB 2013-03-08 8:23 PM neutral  sad 
    3 342521635332_347654842010216 FB 2013-02-22 8:13 PM positive <NA> 
    4 342521635332_567940913224072 FB 2013-02-22 6:27 PM neutral  <NA> 
    5 342521635332_318826431554118 FB 2013-02-22 2:22 PM positive joy 
    6 342521635332_215298638612191 FB 2013-02-20 8:09 PM negative angry 
    7 342521635332_407970722630311 FB 2013-02-15 8:48 PM neutral  joy 

mongo.insert(m,'abc.xyz',pqr) 

當我執行mongo insert,數據越來越插入mongodb但數據看起來像這樣在MongoDB中。

db.xyz.find() 
    { "_id" : ObjectId("5176273533da42cdbe49f2c5"), "V2" : [ 3, 5, 6, 9, 4, 2, 7, 1, 10, 8 ], "V1" : [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ], "V3" : [ 6, 5, 4, 4, 4, 3, 2, 1, 1, 1 ], "V4" : [ 5, 8, 7, 4, 1, 6, 10, 9, 3, 2 ], "polarity" : [ 3, 2, 3, 2, 3, 1, 2, 2, 3, 1 ], "emotion" : [ -2147483648, -2147483648, -2147483648, -2147483648, -2147483648, -2147483648, 1, -2147483648, -2147483648, -2147483648 ] } 

我需要將數據插入類似於表格數據的mongodb中。

+1

嘗試把它變成MongoDB中之前的所有因素轉換爲字符。 – 2013-04-23 06:33:55

+0

澄清'表格數據':你想要以'data.frame'的形式保存結果嗎?如果是這樣,你能給'str(mydata)'一些輸出嗎? – dardisco 2013-04-23 06:59:29

回答

1

先轉換數據幀到BSON--

b=mongo.bson.from.df(df)然後

負載蒙戈集合using--

mongo.insert(mongo,ns,b)