例に示されているエントリを含むデータベースにクエリを実行しています。すべてのエントリには、次の値が含まれています。
_id
overallitem
:との一意のIDplaced_items
name
:ての名前overallitem
loc
:overallitem
との場所placed_items
time_id
:overallitem
保存された時間placed_items
:配列を含むplaced_items
(ゼロplaced_items : [],
から無制限の量までの範囲で指定できます)。category_id
:のカテゴリplaced_items
full_id
:の完全なIDplaced_items
を抽出したいname
、full_id
および制約が与えられたレベルcategory_id
ごとにplaced_items
time_id
loc
データ例:
{
"_id" : "5040",
"name" : "entry1",
"loc" : 1,
"time_id" : 20121001,
"placed_items" : [],
}
{
"_id" : "5041",
"name" : "entry2",
"loc" : 1,
"time_id" : 20121001,
"placed_items" : [
{
"_id" : "5043",
"category_id" : 101,
"full_id" : 901,
},
{
"_id" : "5044",
"category_id" : 102,
"full_id" : 902,
}
],
}
{
"_id" : "5042",
"name" : "entry3",
"loc" : 1,
"time_id" : 20121001,
"placed_items" : [
{
"_id" : "5045",
"category_id" : 101,
"full_id" : 903,
},
],
}
この例で期待される結果は次のとおりです。
"name" "full_id" "category_id"
"entry2" 901 101
"entry2" 902 102
"entry3" 903 101
したがって、placed_items
が空の場合はエントリをデータフレームに配置し、エントリが含まれている場合はplaced_items
エントリをデータフレームn
に配置しますn
RBloggerの例を作成して、目的のデータフレームを作成しようとしました。
#Set up database
mongo <- mongo.create()
#Set up condition
buf <- mongo.bson.buffer.create()
mongo.bson.buffer.append(buf, "loc", 1)
mongo.bson.buffer.start.object(buf, "time_id")
mongo.bson.buffer.append(buf, "$gte", 20120930)
mongo.bson.buffer.append(buf, "$lte", 20121002)
mongo.bson.buffer.finish.object(buf)
query <- mongo.bson.from.buffer(buf)
#Count
count <- mongo.count(mongo, "items_test.overallitem", query)
#Note that these counts don't work, since the count should be based on
#the number of placed_items in the array, and not the number of entries.
#Setup Cursor
cursor <- mongo.find(mongo, "items_test.overallitem", query)
#Create vectors, which will be filled by the while loop
name <- vector("character", count)
full_id<- vector("character", count)
category_id<- vector("character", count)
i <- 1
#Fill vectors
while (mongo.cursor.next(cursor)) {
b <- mongo.cursor.value(cursor)
order_id[i] <- mongo.bson.value(b, "name")
product_id[i] <- mongo.bson.value(b, "placed_items.full_id")
category_id[i] <- mongo.bson.value(b, "placed_items.category_id")
i <- i + 1
}
#Convert to dataframe
results <- as.data.frame(list(name=name, full_id=full_uid, category_id=category_id))
overallitem
レベル(つまり_id
または)で値を抽出したいがname
、レベルで情報を収集できない場合は、条件が機能し、コードが機能しplaced_items
ます。さらに、点線で抽出する必要がfull_id
あり、category_id
機能していないようです。誰か助けてもらえますか?