如何获取mongodb

时间:2016-03-10 18:45:36

标签: json mongodb

如何使用mongoshell

从json文件中获取数据

我希望通过policyID来处理数据 在json文件中说我发送的PolicyID是3148

我尝试过编写命令的方法,但是说取了0行。

db.GeneralLiability.find({"properties.id":"21281"})
db.GeneralLiability.find({properties:{_id:"21281"}})

我是否需要设置其他任何内容?索引,游标等?

示例json

{
    "session": {
        "data": {
            "account": {
            "properties": {
            "userName": "abc.com",
            "_dateModified": "2014-10-01",
            "_manuscript": "Carrier_New_Rules_2_1_0",
            "_engineVersion": "2.0.0",
            "_cultureCode": "en-US",
            "_cultureName": "United States [english]",
            "_context": "Underwriter",
            "_caption": "Carrier New  Rules  (2.1.0)",
            "_id": "p1CEB08012E51477C9CD0E89FE77F5E51"
        },
         "properties": {
        "_xmlns:xsd": "http://www.w3.org/2001/XMLSchema",
        "_xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
        "_id": "3148",
        "_HistoryID": "5922",
        "_Type": "onset",
        "_Datestamp": "2014-10-01T04:46:33",
        "_TransactionType": "New",
        "_EffectiveDate": "2014-01-01",
        "_Charge": "1599",
        "_TransactionGroup": "t4CE4FA751F9C400D9007E692A883DA66",
        "_PolicyID": "3148",
        "_Index": "1",
        "_Count": "1",
        "_Sequence": "1"
    }
}
}

2 个答案:

答案 0 :(得分:0)

您的文档格式存在问题。首先,我很确定使用下划线是为mongo保留的(我可能是错的)。无论哪种方式,它都是糟糕的形式。我已经为您重新构建了数据。我不确定你为什么要如此嵌套你的数据,但我猜你有充分的理由。

您会注意到我使用Mongo的ObjectID作为我的_id:

{ 
"_id" : ObjectId("56e1c1f53bac31a328e3682b"), 
"session" : {
    "data" : {
        "account" : {
            "properties" : {
                "xmlns:xsd" : "http://www.w3.org/2001/XMLSchema", 
                "xmlns:xsi" : "http://www.w3.org/2001/XMLSchema-instance", 
                "HistoryID" : "5922", 
                "Type" : "onset", 
                "Datestamp" : "2014-10-01T04:46:33", 
                "TransactionType" : "New", 
                "EffectiveDate" : "2014-01-01", 
                "Charge" : "1599", 
                "TransactionGroup" : "t4CE4FA751F9C400D9007E692A883DA66", 
                "PolicyID" : "3148", 
                "Index" : "1", 
                "Count" : "1", 
                "Sequence" : "1"
            }
        }
    }
}
}

现在,如果您运行此命令,它将返回您的文档:

{ "session.data.account.properties.PolicyID": "3148" }

答案 1 :(得分:0)

这将返回带有_PolicyID = "3148"的文档:

db.GeneralLiability.find({
  "session._PolicyID": "3148"
}).pretty();