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

查看:14
本文介绍了如何高效解决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.

So 提供了 How To Ask a Good Question 但这与 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、内存大小、CPU 数量,甚至时钟速度(如果知道)
  • 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.

示例:

回复:1

在 windows 笔记本电脑上使用 mongo 2.6 我无法收集大于 2GB 的集合,为什么?

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

回复: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
}

回复: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
                }
            ]
        }
    ]
}

回复:4

我的副本集有问题,数据没有使用 mongo 3.2 复制到其他服务器,在 rs.config 转储下面:

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"
          }
       ]
    }

回复: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天全站免登陆