排序多个集合与子文档
Sorting Multiple Collections vs Subdocument
我也是 MongoDB 的 nodejs 新手。很抱歉,如果我问的是一个简单的问题,但我只是在创建结构,所以现在是更改的最佳时机。
我当前的结构(子文档):
var Channels = new Schema({
_id: {type: Schema.Types.Number, ref: 'Channel'},
type: {type: String, default: ''},
subscriber: {type: Boolean, default: false},
Points: {type: Number, default: 0},
mPoints: {type: Number, default: 0},
lastSeen: {type: Date, default: Date.now() }
});
var ViewerSchema = new Schema({
_id: Number,
name: String,
displayName: String,
Channels: [Channels]
});
var Viewer = mongoose.model('Viewer', ViewerSchema, 'Viewers');
从那时起我打算用它做什么。
按Viewer.Channels[id].mPoints
排序,相当于单通道按月积分排序。 (id 不变)
简单示例:
{
"_id" : 46761135,
"name" : "h4ckhers",
"displayName" : "h4ckhers",
"Channels" : [
{
"_id" : 46761135,
"mPoints" : 0,
"Points" : 0,
"subscriber" : false,
"type" : "mod"
},
{
"_id" : 37259814,
"mPoints" : 0,
"Points" : 0,
"subscriber" : false,
"type" : null
},
{
"_id" : 71978007,
"mPoints" : 10,
"Points" : 0,
"subscriber" : true,
"type" : null
}
],
"__v" : 2
},
{
"_id" : 144771688,
"name" : "sevo",
"displayName" : "sevo",
"Channels" : [
{
"_id" : 71978007,
"mPoints" : 5,
"Points" : 0,
"subscriber" : false,
"type" : null
}
],
"__v" : 0
},
{
"_id" : 43354629,
"name" : "son1903",
"displayName" : "Son1903",
"Channels" : [
{
"_id" : 71978007,
"mPoints" : 9,
"Points" : 0,
"subscriber" : false,
"type" : null
}
],
"__v" : 0
}
当我使用 Viewer.Channels['71978007'].mPoint
时,它应该像 (h4ckhers, Son1903, sevo) 这样输出。我该怎么做,我应该为此使用多个文档,如果应该的话,它们应该如何索引。 (我想在 mongodb 中处理它,而不是在 javascript 中处理它,如果它是一个选项)
注意:每位观看者最多可拥有 100 个频道。 (但在现实世界中,观众可能最多拥有 15-20 个频道,平均拥有 5-8 个频道)
我自己找到了解决方案。这是我的查询:
db.Viewers.find( { "Channels._id": 71978007 } ).sort( { "Channels.mPoints": -1 } )
我的指数:
db.Viewers.createIndex( { "Channels._id": 1, "Channels.mPoints": -1 } )
此查询使用的索引:Channels._id_1_Channels.mPoints_-1
参考:https://docs.mongodb.com/manual/core/index-multikey/
旧的部分解决方案:
我找到了这个查询,它正在做这件事,但我仍然想知道性能。
db.Viewers.aggregate([
{$unwind:"$Channels"},
{$match:{"Channels._id":71978007}},
{$sort:{"Channels.mPoints":-1}}])
我为那个查询创建了这个索引:
db.Viewers.createIndex( { "Channels._id": 1, "Channels.mPoints": -1 } )
但是当我使用
db.Viewers.aggregate( [ { $indexStats: { } } ] ).pretty()
响应是:
{
"name" : "Channels._id_1_Channels.mPoints_-1",
"key" : {
"Channels._id" : 1.0,
"Channels.mPoints" : -1.0
},
"host" : "Hackhers:27017",
"accesses" : {
"ops" : NumberLong(0),
"since" : ISODate("2017-01-21T18:46:53.764Z")
}
}
我也是 MongoDB 的 nodejs 新手。很抱歉,如果我问的是一个简单的问题,但我只是在创建结构,所以现在是更改的最佳时机。
我当前的结构(子文档):
var Channels = new Schema({
_id: {type: Schema.Types.Number, ref: 'Channel'},
type: {type: String, default: ''},
subscriber: {type: Boolean, default: false},
Points: {type: Number, default: 0},
mPoints: {type: Number, default: 0},
lastSeen: {type: Date, default: Date.now() }
});
var ViewerSchema = new Schema({
_id: Number,
name: String,
displayName: String,
Channels: [Channels]
});
var Viewer = mongoose.model('Viewer', ViewerSchema, 'Viewers');
从那时起我打算用它做什么。
按Viewer.Channels[id].mPoints
排序,相当于单通道按月积分排序。 (id 不变)
简单示例:
{
"_id" : 46761135,
"name" : "h4ckhers",
"displayName" : "h4ckhers",
"Channels" : [
{
"_id" : 46761135,
"mPoints" : 0,
"Points" : 0,
"subscriber" : false,
"type" : "mod"
},
{
"_id" : 37259814,
"mPoints" : 0,
"Points" : 0,
"subscriber" : false,
"type" : null
},
{
"_id" : 71978007,
"mPoints" : 10,
"Points" : 0,
"subscriber" : true,
"type" : null
}
],
"__v" : 2
},
{
"_id" : 144771688,
"name" : "sevo",
"displayName" : "sevo",
"Channels" : [
{
"_id" : 71978007,
"mPoints" : 5,
"Points" : 0,
"subscriber" : false,
"type" : null
}
],
"__v" : 0
},
{
"_id" : 43354629,
"name" : "son1903",
"displayName" : "Son1903",
"Channels" : [
{
"_id" : 71978007,
"mPoints" : 9,
"Points" : 0,
"subscriber" : false,
"type" : null
}
],
"__v" : 0
}
当我使用 Viewer.Channels['71978007'].mPoint
时,它应该像 (h4ckhers, Son1903, sevo) 这样输出。我该怎么做,我应该为此使用多个文档,如果应该的话,它们应该如何索引。 (我想在 mongodb 中处理它,而不是在 javascript 中处理它,如果它是一个选项)
注意:每位观看者最多可拥有 100 个频道。 (但在现实世界中,观众可能最多拥有 15-20 个频道,平均拥有 5-8 个频道)
我自己找到了解决方案。这是我的查询:
db.Viewers.find( { "Channels._id": 71978007 } ).sort( { "Channels.mPoints": -1 } )
我的指数:
db.Viewers.createIndex( { "Channels._id": 1, "Channels.mPoints": -1 } )
此查询使用的索引:Channels._id_1_Channels.mPoints_-1
参考:https://docs.mongodb.com/manual/core/index-multikey/
旧的部分解决方案:
我找到了这个查询,它正在做这件事,但我仍然想知道性能。
db.Viewers.aggregate([
{$unwind:"$Channels"},
{$match:{"Channels._id":71978007}},
{$sort:{"Channels.mPoints":-1}}])
我为那个查询创建了这个索引:
db.Viewers.createIndex( { "Channels._id": 1, "Channels.mPoints": -1 } )
但是当我使用
db.Viewers.aggregate( [ { $indexStats: { } } ] ).pretty()
响应是:
{
"name" : "Channels._id_1_Channels.mPoints_-1",
"key" : {
"Channels._id" : 1.0,
"Channels.mPoints" : -1.0
},
"host" : "Hackhers:27017",
"accesses" : {
"ops" : NumberLong(0),
"since" : ISODate("2017-01-21T18:46:53.764Z")
}
}