Mongodb 索引和猫鼬
Mongodb Index & mongoose
我有一个更新查询需要 运行,我发现它很慢。
const nowplayingData = {"type":"S","station": req.params.stationname, "song": data[1], "artist": data[0], "timeplay":npdate};
LNowPlaying.findOneAndUpdate(
nowplayingData,
{ $addToSet: { history: [uuid] } }, { upsert: true }, function(err) {
if (err) {
console.log('ERROR when submitting round');
console.log(err);
}
});
文档看起来像这样
{
"_id": {
"$oid": "5f117ae15b43e19dabd5ffb0"
},
"artist": "Ewa Kupec Piano",
"song": "Nocturne In E-Flat Major, Op 7 Fourth Movement (Rondo",
"station": "1lifeRadio",
"timeplay": {
"$date": "2020-07-17T11:19:00Z"
},
"type": "S",
"__v": 0,
"history": [
"7320564F-76B2-40D0-A0E8-E3917148F567"
]
}
我已经在 Mongodb 指南针应用程序中创建了索引
但是我注意到它在更新时没有使用索引。我需要在 MongooseJS 中做些什么才能使用索引吗?
我的架构是
const StationNowPlayingSchema = new mongoose.Schema({
station: {
type: String,
// index: { unique: true }
},
artist: {
type: String,
// index: { unique: true }
},
song: {
type: String,
// index: { unique: true }
},
timeplay: {
type: Date,
},
type:{ type: String},
cover:{ type: String},
url:{ type: String},
history:[],
}, { collection: 'NowPlaying'});
不太清楚你是创建了单独的索引还是一个复合索引,但假设你有复合索引,你的搜索顺序似乎没有映射到创建的索引:
"type":X,"station": Y, "song": Z, "artist":M, "timeplay":N
索引:
artist,song,station,type,timeplay
根据您的复合索引,需要使用以下搜索顺序组合:
artist,song,station,type,timeplay
artist,song,station,type
artist,song,station
artist,song
artist
然后 -> 是 -> 那些 metter 的命令! :)
但我的建议是仅将最具选择性的字段用于索引并分析您的搜索查询或至少使用最多的查询并根据这些查询创建索引...
我有一个更新查询需要 运行,我发现它很慢。
const nowplayingData = {"type":"S","station": req.params.stationname, "song": data[1], "artist": data[0], "timeplay":npdate};
LNowPlaying.findOneAndUpdate(
nowplayingData,
{ $addToSet: { history: [uuid] } }, { upsert: true }, function(err) {
if (err) {
console.log('ERROR when submitting round');
console.log(err);
}
});
文档看起来像这样
{
"_id": {
"$oid": "5f117ae15b43e19dabd5ffb0"
},
"artist": "Ewa Kupec Piano",
"song": "Nocturne In E-Flat Major, Op 7 Fourth Movement (Rondo",
"station": "1lifeRadio",
"timeplay": {
"$date": "2020-07-17T11:19:00Z"
},
"type": "S",
"__v": 0,
"history": [
"7320564F-76B2-40D0-A0E8-E3917148F567"
]
}
我已经在 Mongodb 指南针应用程序中创建了索引
但是我注意到它在更新时没有使用索引。我需要在 MongooseJS 中做些什么才能使用索引吗?
我的架构是
const StationNowPlayingSchema = new mongoose.Schema({
station: {
type: String,
// index: { unique: true }
},
artist: {
type: String,
// index: { unique: true }
},
song: {
type: String,
// index: { unique: true }
},
timeplay: {
type: Date,
},
type:{ type: String},
cover:{ type: String},
url:{ type: String},
history:[],
}, { collection: 'NowPlaying'});
不太清楚你是创建了单独的索引还是一个复合索引,但假设你有复合索引,你的搜索顺序似乎没有映射到创建的索引:
"type":X,"station": Y, "song": Z, "artist":M, "timeplay":N
索引:
artist,song,station,type,timeplay
根据您的复合索引,需要使用以下搜索顺序组合:
artist,song,station,type,timeplay
artist,song,station,type
artist,song,station
artist,song
artist
然后 -> 是 -> 那些 metter 的命令! :)
但我的建议是仅将最具选择性的字段用于索引并分析您的搜索查询或至少使用最多的查询并根据这些查询创建索引...