【问题标题】:How to get rid of the mongo IDs when converting a collection to json将集合转换为 json 时如何摆脱 mongo ID
【发布时间】:2015-12-02 00:37:15
【问题描述】:

我正在使用 mongo java API 将集合转换为 json:

MongoCollection<Document> coll = db.getCollection("day_EURUSD");
FindIterable<Document> fi = coll.find();
System.out.println(fi.first().toJson());

但是结果仍然包含 nongoDB 'clutter':

{ "_id" : { "$oid" : "565d90808b821237efdc39cb" }, "currencyPairs" : [{ "a....

我怎样才能优雅地摆脱 _id 和 $oid 以便我回到“正常”json?

谢谢

【问题讨论】:

    标签: java mongodb


    【解决方案1】:

    试试这个:

       MongoCollection<Document> coll = db.getCollection("day_EURUSD");
        FindIterable<Document> fi = coll.find();
        fi.forEach(new Block<Document>() {
            @Override
            public void apply(final Document document) {
                        // Suppress the DB Id column of the query result.
                   document.remove("_id");
            }
        });
        ...
    

    【讨论】:

      猜你喜欢
      • 2017-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-01
      • 1970-01-01
      相关资源
      最近更新 更多