【问题标题】:MongoDB Java - Fetching id in nested jsonMongoDB Java - 在嵌套的 json 中获取 id
【发布时间】:2017-04-14 15:03:00
【问题描述】:

我有以下 json 结构。我正在尝试在 hData._id 不为空的 java 中运行以下 mongo 查询。

MongoDb Query: db.Collection.find({},{"hData._id":1, "hData.createdBy":1} )

{
    "_id" : ObjectId("55567e594e3256a23565ce58"),
       "hData" : {
        "isDeleted" : false,
        "canDelete" : false,
        "canUpdate" : false,
        "createdBy" : “xyz”,
        "createdDate" : "2015-05-15T15:05:30",
        "_id" : "7"
    },
    "changeDate" : "2015-02-19T16:02:12",

}

我用java编写的获取hData._id的代码是

MongoCursor<Document> cur = col.find(new BasicDBObject("hData._id", new BasicDBObject("$ne",null)))).iterator();
        try{
            while(cur.hasNext()){
                System.out.println(cur.next().getObjectId("hData._id"));
                i++;
            }
        }finally {
            cur.close();
        }

但是,hData._id 返回为null。你能帮我解决这个问题吗?

【问题讨论】:

  • 你检查过 cur.next() 是什么吗?我认为你不能调用 getObjectId("hData._id") 。
  • 你用的是哪个版本的mongo驱动?

标签: java mongodb mongodb-query spring-data-mongodb


【解决方案1】:

您无法使用点表示法获取嵌套属性,例如x.y.

因此,在您的示例中,您需要先获取hData,然后在_id 上调用get。像这样:

    MongoCursor<Document> cur = col.find(new BasicDBObject("hData._id", new BasicDBObject("$ne",null))).iterator();

    while(cur.hasNext()){
        System.out.println(cur.next().get("hData", Document.class).getString("_id"));
    }

还要注意,在您的示例中,hData._id 显示为字符串而不是 ObjectId,因此在我的示例中我使用了getString()

编辑 因为听起来您可能对 hData._id 有混合类型,所以这里有一个更强大的示例,其中包含类型检查和一些额外的调试输出来说明:

    MongoCursor<Document> cur = col.find(new BasicDBObject("hData._id", new BasicDBObject("$ne",null))).iterator();

    while(cur.hasNext()){
        Document doc = cur.next();
        System.out.println("Document _id" + doc.get("_id"));
        Document hdata = doc.get("hData", Document.class);
        Object id = hdata.get("_id");
        System.out.println("hData._id " + id);

        // check type if you need to
        if (id instanceof String) {
            System.out.println("hData._id is String: " + id);
        } else if (id instanceof ObjectId) {
            System.out.println("hData._id is ObjectId: " + id);
        } else {
            System.out.println("hData._id is of type " + id.getClass().getName());
        }
    }

【讨论】:

  • 嗨@helmy - 我仍在获取某些行 - ObjectId 无法转换为 java.lang.string
  • 看起来您的数据中有混合类型。可能希望保持您的数据一致。无论如何,我已经添加了另一个关于如何检查和处理不同类型的值的示例。
  • 感谢@helmy 的编辑。您的解决方案有助于解决此问题。再次感谢!
【解决方案2】:

您可以使用FiltersProjections 辅助方法。

  try (MongoCursor<Document> cur  = coll.find(Filters.ne("hData._id", null)).projection(Projections.include("hData._id", "hData.createdBy")).iterator()) {
         while(cur.hasNext()){
              Document doc = cur.next();
              Document hData = doc.get("hData", Document.class);
              String id = hData.getString("_id");
              String createdBy = hData.getString("createdBy");
        }
   }

【讨论】:

  • 感谢@Veeram,我如何处理 -ObjectId 无法转换为 java.lang.string ?
  • Np。所有 id 的对象都是 Id 的吗?如果是,则使用ObjectId id = hData.getObjectId("_id"),否则先修复数据。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-01
相关资源
最近更新 更多