如何有效解决mongoDB相关问题? [英] How to solve mongoDB related issue efficiently?

查看:65
本文介绍了如何有效解决mongoDB相关问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

monoDB标记中有很多关于不同问题的问题,我看到也有一些评论要求类似的数据.

There is a lot of questions in monoDB tag about different problems, and I see that there is also some bunch of comments requesting similar data.

因此提供了如何问一个好问题,但这与MongoDB没有关系. 是否有任何好的文件可以让我提出很好的有价值的问题?

So provides How To Ask a Good Question but this is not related to MongoDB. Is there any good document which allow me to ask good and valuable question?

推荐答案

有一些规则可以帮助您获得有关MongoDB相关问题的良好且有价值的答案.

There is a few rules that will help here to get good and valuable answer for MongoDB related question.

请在下面查看一些常见的类别和步骤,这些类别和步骤将有助于收集数据,这些数据可以帮助您更快地找到一个好的答案.

Please see below some common categories and steps that will help with gathering data which could help you to find a good answer faster.

  1. 基本-随着mongoDB的发展,一些凉爽的功能可在更高版本中使用-为避免混淆,请提供您当前的mongo版本,并告知我们这是独立系统,副本集还是分片环境

  1. Basics - as mongoDB evolves some cool functions are available in higher version - to avoid confusion please give your current mongo version and let us know if this is standalone system, replica set or sharded environment

有关性能的问题:

  • 请提供执行状态输出-用于查询:db.collection.find({query}).explain("executionStats")-将提供有关查询,索引的统计信息,用于聚合框架:db.collection.aggregate([{pieplineDatausedToExecuteAggregation},{explain:true}])
  • ssd,ram大小,cpus no甚至时钟速度(如果已知)之类的硬件规格
  • please provide execution stats output - for queries: db.collection.find({query}).explain("executionStats") - that will give some statistics about query, indexes, for aggregation framework: db.collection.aggregate([{pieplineDatausedToExecuteAggregation},{explain:true}])
  • hardware specs like ssd, ram size, cpus no and even clock speed if known

数据处理-由于查询基于文档结构,因此请提供有效的文档转储(或多个),并确保mocked字段反映了查询中的字段,有时在尝试构建查询时,无法插入示例文档,因为它们的结构无效.另外,如果您希望在过程p的结果上能有一定结果-请附上期望的示例.

Data manipulation - as queries are based on document structure please provide valid document dump (or even more than one) and ensure that mocked fields are reflecting fields in query, sometimes when trying to build query, we are unable to insert example documents as they structure isn't valid. Also if you are expecting certain result at the ned of process p - please attach expected example.

复制集/分片问题-请添加rs.config()/sh.status()并删除主机数据(如果敏感)

Replica set/sharding issues - please add rs.config() / sh.status() and remove host data (if sensitive)

如果您对驱动程序/框架有疑问,请显示已完成的操作以及在哪里有问题.有时很难将查询从mongo shell语法转换为驱动程序/框架语法-因此,如果您可以尝试在mongoDB shell中构建该查询-并有运行示例,请将其添加到问题中.

If you have driver/framework specific question - please display what was done and where are you have problem. Sometimes it is very hard to translate query from mongo shell syntax to driver/framework syntax - so if you could try to build that query in mongoDB shell - and having running example - please add it to question.

示例:

RE:1

Examples:

RE:1

在Windows笔记本电脑上使用mongo 2.6我无法收集超过2GB的内存,为什么?

Using mongo 2.6 on windows laptop I am unable to have collection greater than 2GB, why?

RE:2

我的查询db.collection.find({isValid:true})耗时超过30秒,请参阅说明输出:

My query db.collection.find({isValid:true}) takes more than 30 seconds, please see explain output:

{
    "queryPlanner" : {
        "plannerVersion" : 1,
        "namespace" : "test.collectionName",
        "indexFilterSet" : false,
        "parsedQuery" : {},
        "winningPlan" : {
            "stage" : "COLLSCAN",
            "direction" : "forward"
        },
        "rejectedPlans" : []
    },
    "executionStats" : {
        "executionSuccess" : true,
        "nReturned" : 6,
        "executionTimeMillis" : 0,
        "totalKeysExamined" : 0,
        "totalDocsExamined" : 6,
        "executionStages" : {
            "stage" : "COLLSCAN",
            "nReturned" : 6,
            "executionTimeMillisEstimate" : 0,
            "works" : 8,
            "advanced" : 6,
            "needTime" : 1,
            "needYield" : 0,
            "saveState" : 0,
            "restoreState" : 0,
            "isEOF" : 1,
            "invalidates" : 0,
            "direction" : "forward",
            "docsExamined" : 6
        }
    },
    "serverInfo" : {
        "host" : "greg",
        "port" : 27017,
        "version" : "3.3.6-229-ge533634",
        "gitVersion" : "e533634d86aae9385d9bdd94e15d992c4c8de622"
    },
    "ok" : 1.0
}

RE:3

我无法从聚合管道mongo 3.2.3的每个记录中获取最后3个数组元素

I'm having trouble to get last 3 array elements from every record in my aggregation pipeline, mongo 3.2.3

我的查询:db.collection.aggregate([{aggregation pipeline}])

文档架构:

{
    "_id" : "john",
    "items" : [{
            "name" : "John",
            "items" : [{
                    "school" : ObjectId("56de35ab520fc05b2fa3d5e4"),
                    "grad" : true
                }
            ]
        }, {
            "name" : "John",
            "items" : [{
                    "school" : ObjectId("56de35ab520fc05b2fa3d5e5"),
                    "grad" : true
                }
            ]
        }, {
            "name" : "John",
            "items" : [{
                    "school" : ObjectId("56de35ab520fc05b2fa3d5e6"),
                    "grad" : true
                }
            ]
        }, {
            "name" : "John",
            "items" : [{
                    "school" : ObjectId("56de35ab520fc05b2fa3d5e7"),
                    "grad" : true
                }
            ]
        }, {
            "name" : "John",
            "items" : [{
                    "school" : ObjectId("56de35ab520fc05b2fa3d5e8"),
                    "grad" : true
                }
            ]
        }
    ]
}

//expected result

{
    "_id" : "john",
    "items" : [{
            "name" : "John",
            "items" : [{
                    "school" : ObjectId("56de35ab520fc05b2fa3d5e4"),
                    "grad" : true
                }
            ]
        }, {
            "name" : "John",
            "items" : [{
                    "school" : ObjectId("56de35ab520fc05b2fa3d5e5"),
                    "grad" : true
                }
            ]
        }, {
            "name" : "John",
            "items" : [{
                    "school" : ObjectId("56de35ab520fc05b2fa3d5e6"),
                    "grad" : true
                }
            ]
        }
    ]
}

RE:4

我的副本集出现问题,使用rs.config dump下面的mongo 3.2无法将数据复制到其他服务器:

I have issues with my replica set, data is not replicated to other server using mongo 3.2, below rs.config dump:

   {
       "_id" : "rs0",
       "version" : 1,
       "members" : [
          {
             "_id" : 1,
             "host" : "mongodb0.example.net:27017"
          }
       ]
    }

RE:5

我在mongo中有聚合查询,无法从c#驱动程序获取键入的结果

I have aggregation query in mongo and having trouble to get typed result from c# driver

startDate = new Date() // Current date
startDate.setDate(startDate.getDate() - 7) // Subtract 7 days

db.collection.aggregate([{
            $match : {
                LastUpdate : {
                    $gte : startDate
                }
            }
        }, {
            $sort : {
                LastUpdate : -1
            }
        }, //sort data
        {
            $group : {
                _id : "$Emp_ID",
                documents : {
                    $push : "$$ROOT"
                }
            }
        }, {
            $project : {
                _id : 1,
                documents : {
                    $slice : ["$documents", 3]
                }
            }
        }
    ])

我的C#代码

public static void Main()
{
    var client = new MongoClient("mongodb://localhost:27017");
    var database = client.GetDatabase("test");

    var collection = database.GetCollection<InnerDocument>("irpunch");


    var aggregationDocument = collection.Aggregate()
        .Match(x=>x.LastUpdate> DateTime.Now.AddDays(-40))
        .SortByDescending(x => x.LastUpdate)
        .Group(BsonDocument.Parse("{ _id:'$Emp_ID', documents:{ '$push':'$$ROOT'}}"))
        // how to get projection result as typed object ??
        .Project(BsonDocument.Parse("{ _id:1, documents:{ $slice:['$documents', 3]}}")).ToList();


    }
}

这篇关于如何有效解决mongoDB相关问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆