Robo 3T MongoDB不是主要的

时间:2018-10-31 17:06:37

标签: mongodb mongodb-query aggregation-framework

我需要运行一个聚合查询,该查询需要将一些文档输出到同一数据库上的另一个集合。我目前遇到的问题是,没有$ out阶段,查询就可以了,但是当我进入$ out阶段时,它只会给我以下错误: enter image description here

这是查询:

use catchem
db.quests.aggregate([
    { $match: { distanceFromStage: { $lte: 50 }}},
    { $sort: { stageId: 1, questId: 1, distanceFromStage: -1}},
    { $group: {
        _id: "$stageId",
        measurements: {$push: {
            questId: "$questId",
            stageId: "$stageId",
            hartslag: "$hartslag",
            decibelmeter: "$decibelmeter",
            distanceFromStage: "$distanceFromStage",
            stageLoc: "$stageLoc"
        }}
    }},
    { $unwind: "$measurements"},
    { $bucket: {
        groupBy: "$measurements.distanceFromStage",
        boundaries: [0, 10, 20, 30, 40, 50],
        output: { 
            stagesByDistance: { $push: {      
                questId: "$measurements.questId",
                stageId: "$measurements.stageId",
                hartslag: "$measurements.hartslag",
                decibelmeter: "$measurements.decibelmeter",
                distanceFromStage: "$measurements.distanceFromStage",
                stageLoc: "$measurements.stageLoc"
            }}
        }
    }},
    { $unwind:"$stagesByDistance"},
    { $group: {
        _id: {stageId: "$stagesByDistance.stageId", distance: "$_id"},
        avgBpm: {$avg: "$stagesByDistance.hartslag"},
        avgDb: {$avg: "$stagesByDistance.decibelmeter"},
    }},
    { $sort: {_id: 1}},
    { $group: {
        _id: "$_id.stageId",
        avgs: { $push: {
            distance: "$_id.distance",
            avgBpm: "$avgBpm",
            avgDb: "$avgDb"
        }}
    }},
    { $project: {
        _id: 0,
        stageId: "$_id",
        avgBpm10: {$arrayElemAt: ["$avgs.avgBpm", 0]},
        avgBpm20: {$arrayElemAt: ["$avgs.avgBpm", 1]},
        avgBpm30: {$arrayElemAt: ["$avgs.avgBpm", 2]},
        avgBpm40: {$arrayElemAt: ["$avgs.avgBpm", 3]},
        avgBpm50: {$arrayElemAt: ["$avgs.avgBpm", 4]},
        avgDb10: {$arrayElemAt: ["$avgs.avgDb", 0]},
        avgDb20: {$arrayElemAt: ["$avgs.avgDb", 1]},
        avgDb30: {$arrayElemAt: ["$avgs.avgDb", 2]},
        avgDb40: {$arrayElemAt: ["$avgs.avgDb", 3]},
        avgDb50: {$arrayElemAt: ["$avgs.avgDb", 4]}
    }},
    { $sort: { stageId: 1}},
    { $out: "heatmaps" }
],{allowDiskUse:true})

这是sh.status(true):

sharding version: {
    "_id" : 1,
    "minCompatibleVersion" : 5,
    "currentVersion" : 6,
    "clusterId" : ObjectId("5bd483c5736ad7af3ebb1b9b")
}
  shards:
    {  "_id" : "Catchem_RS1",  "host" : "Catchem_RS1/localhost:27030,localhost:27031,localhost:27032",  "state" : 1 }
    {  "_id" : "Catchem_RS2",  "host" : "Catchem_RS2/localhost:27033,localhost:27034,localhost:27035",  "state" : 1 }
  active mongoses:
    {  "_id" : "Laptop-Cedric:27039",  "advisoryHostFQDNs" : [ ],  "mongoVersion" : "4.0.3",  "ping" : ISODate("2018-10-31T16:49:16.455Z"),  "up" : NumberLong(3920),  "waiting" : true }
 autosplit:
    Currently enabled: yes
  balancer:
    Currently enabled:  yes
    Currently running:  no
    Failed balancer rounds in last 5 attempts:  2
    Last reported error:  Could not find host matching read preference { mode: "primary" } for set Catchem_RS2
    Time of Reported error:  Mon Oct 29 2018 17:13:28 GMT+0100 (Romance Standard Time)
    Migration Results for the last 24 hours: 
        No recent migrations
  databases:
    {  "_id" : "catchem",  "primary" : "Catchem_RS1",  "partitioned" : true,  "version" : {  "uuid" : BinData(4,"blaviOp/SLC+zhJTT74pbg=="),  "lastMod" : 1 } }
        catchem.quests
            shard key: { "questId" : 1 }
            unique: false
            balancing: true
            chunks:
                undefined   undefined
                undefined   undefined
            { "questId" : { "$minKey" : 1 } } -->> { "questId" : 130 } on : Catchem_RS2 Timestamp(2, 0) 
            { "questId" : 130 } -->> { "questId" : 259 } on : Catchem_RS2 Timestamp(3, 0) 
            { "questId" : 259 } -->> { "questId" : 388 } on : Catchem_RS2 Timestamp(4, 0) 
            { "questId" : 388 } -->> { "questId" : 517 } on : Catchem_RS2 Timestamp(5, 0) 
            { "questId" : 517 } -->> { "questId" : 646 } on : Catchem_RS2 Timestamp(6, 0) 
            { "questId" : 646 } -->> { "questId" : 775 } on : Catchem_RS2 Timestamp(7, 0) 
            { "questId" : 775 } -->> { "questId" : 905 } on : Catchem_RS2 Timestamp(8, 0) 
            { "questId" : 905 } -->> { "questId" : 1034 } on : Catchem_RS2 Timestamp(9, 0) 
            { "questId" : 1034 } -->> { "questId" : 1163 } on : Catchem_RS1 Timestamp(9, 1) 
            { "questId" : 1163 } -->> { "questId" : 1292 } on : Catchem_RS1 Timestamp(1, 9) 
            { "questId" : 1292 } -->> { "questId" : 1421 } on : Catchem_RS1 Timestamp(1, 10) 
            { "questId" : 1421 } -->> { "questId" : 1550 } on : Catchem_RS1 Timestamp(1, 11) 
            { "questId" : 1550 } -->> { "questId" : 1680 } on : Catchem_RS1 Timestamp(1, 12) 
            { "questId" : 1680 } -->> { "questId" : 1809 } on : Catchem_RS1 Timestamp(1, 13) 
            { "questId" : 1809 } -->> { "questId" : 1938 } on : Catchem_RS1 Timestamp(1, 14) 
            { "questId" : 1938 } -->> { "questId" : { "$maxKey" : 1 } } on : Catchem_RS1 Timestamp(1, 15) 

localhost:27030,localhost:27033是相应副本集的主要节点。

1 个答案:

答案 0 :(得分:0)

  • $ out不能在辅助节点中使用。
  • 不能将分片集合指定为输出集合。

通过mongos运行聚合而不是指向辅助节点应该可以解决此问题。