我在mongo-db中存储了诸如字符串,arraylist,objectId,date等之类的字段。我只想获取Java中具有字符串数据类型的字段。到目前为止,我已经编写了以下代码:
MongoCollection<Document> mongoCollection = database.getCollection(collectionName);
Iterable<Document> fields = mongoCollection.find();
但是它返回集合中的所有字段,而与数据类型无关,即String是否还有其他方法来获取数据类型String的字段?
$type
是您要寻找的运营商。
Iterable<Document> fields =
mongoCollection.find(new Document("field", new Document("$type", "string")));
db.collection.aggregate([
{
"$project": {
"data": {//Reshape as fields are unknown
"$objectToArray": "$$ROOT"
}
}
},
{//Denormalize
$unwind: "$data"
},
{
"$match": {//Find string fields
"data.v": {
$type: "string"
}
}
},
{
"$project": {//Filter the keys
"key": "$data.k"
}
}
])
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句