MongoDB再帰、集約テスト
2320 ワード
MongoDB再帰、集約、ネストテスト
1.再帰テスト
≪テスト・ターゲット|Test Targets|emdw≫:ユーザーのすべてのサブユーザーを再帰的に問合せます.テスト環境:MongoDB 3.2.0
テストデータの挿入db.categories.insert({_id: "MongoDB", parent: "Databases" } )
db.categories.insert({_id: "dbm", parent: "Databases" } )
db.categories.insert({_id: "Databases", parent: "Programming" } )
db.categories.insert({_id: "Languages", parent: "Programming" } )
db.categories.insert( { _id: "Programming", parent: "Books" } )
db.categories.insert( { _id: "Books", parent: null } )
Programmingの子孫を読み込むfunction findChildren(parent) {
var myCursor = db.categories.find({"parent":parent});
while(myCursor.hasNext()) {
var myJsonString = tojson(myCursor.next());
print(myJsonString);
var myJsonObject = JSON.parse(myJsonString);
findChildren(myJsonObject._id);
}
}
findChildren("Programming");
2.集約テスト
統計サイクルとデバイスリストを指定し、デバイスデータを統計します.db.car.insert({deviceId:'111111',fuelConsum:5,mileage:1000,createTime:"2011-10-29 0:0:1"})
db.car.insert({deviceId:'222222',fuelConsum:6,mileage:2000,createTime:"2012-10-29 0:0:1"})
db.car.insert({deviceId:'333333',fuelConsum:7,mileage:3000,createTime:"2013-10-29 0:0:1"})
db.car.insert({deviceId:'444444',fuelConsum:8,mileage:4000,createTime:"2014-10-29 0:0:1"})
db.car.insert({deviceId:'555555',fuelConsum:9,mileage:5000,createTime:"2015-10-29 0:0:1"})
db.car.insert({deviceId:'666666',fuelConsum:10,mileage:6000,createTime:"2016-10-29 0:0:1"})
統計平均燃費統計周期:2012-2016年統計設備リスト:["111111","222222","333333","444444","555555"]var deviceList = ["111111","222222","333333", "444444", "555555"];
db.car.aggregate([
{$match:{"$and":[
{"createTime":{"$gt":"2012-1-1 0:0:0"}},
{"createTime":{"$lt":"2016-12-31 0:0:0"}},
{"deviceId":{$in:deviceList}}]}},
{$group:{_id:null,avg_fuel:{$avg:"$fuelConsum"}}}
])
db.categories.insert({_id: "MongoDB", parent: "Databases" } )
db.categories.insert({_id: "dbm", parent: "Databases" } )
db.categories.insert({_id: "Databases", parent: "Programming" } )
db.categories.insert({_id: "Languages", parent: "Programming" } )
db.categories.insert( { _id: "Programming", parent: "Books" } )
db.categories.insert( { _id: "Books", parent: null } )
function findChildren(parent) {
var myCursor = db.categories.find({"parent":parent});
while(myCursor.hasNext()) {
var myJsonString = tojson(myCursor.next());
print(myJsonString);
var myJsonObject = JSON.parse(myJsonString);
findChildren(myJsonObject._id);
}
}
findChildren("Programming");
db.car.insert({deviceId:'111111',fuelConsum:5,mileage:1000,createTime:"2011-10-29 0:0:1"})
db.car.insert({deviceId:'222222',fuelConsum:6,mileage:2000,createTime:"2012-10-29 0:0:1"})
db.car.insert({deviceId:'333333',fuelConsum:7,mileage:3000,createTime:"2013-10-29 0:0:1"})
db.car.insert({deviceId:'444444',fuelConsum:8,mileage:4000,createTime:"2014-10-29 0:0:1"})
db.car.insert({deviceId:'555555',fuelConsum:9,mileage:5000,createTime:"2015-10-29 0:0:1"})
db.car.insert({deviceId:'666666',fuelConsum:10,mileage:6000,createTime:"2016-10-29 0:0:1"})
var deviceList = ["111111","222222","333333", "444444", "555555"];
db.car.aggregate([
{$match:{"$and":[
{"createTime":{"$gt":"2012-1-1 0:0:0"}},
{"createTime":{"$lt":"2016-12-31 0:0:0"}},
{"deviceId":{$in:deviceList}}]}},
{$group:{_id:null,avg_fuel:{$avg:"$fuelConsum"}}}
])