使用带有投影的find()方法使用mongodb java driver 3.4检索数据 [英] retrieving data with mongodb java driver 3.4 using find()-method with projection
问题描述
我正在使用mongodb java驱动程序3.4.
I am using mongodb java driver 3.4.
在mongodb数据库中,文件根据以下结构保存:
In the mongodb database documents are saved according to the following structure:
{
"_id" : ObjectId("595a9fc4fe3f36402b7edf0e"),
"id" : "123",
"priceInfo" : [
{object1: value1}, {object2: value2}, {object3: value3}
]
}
为了检索具有特定ID的文档的"priceInfo"-数组,我编写了以下代码:
In order to retrieve the "priceInfo"-Array of a Document with a specific id, I wrote the following code:
collection.find(eq("id", id)).first().projection(fields(include("priceInfo"), excludeId()));
我也是根据文档编写此代码的,您可以在这里找到:
I wrote this code according too the documentation, which you can find here:
http: //mongodb.github.io/mongo-java-driver/3.4/javadoc/?com/mongodb/client/model/Projections.html
问题是我的IDE不接受此代码.
The problem is that my IDE won't accept this code.
它给我以下错误指示:
我不知道为什么这段代码不起作用.最初,IDE建议包括几个类,而我确实做到了.但是之后,我仍然收到错误指示,即上面看到的错误指示.
I have no clue why this code doesn't work. At first the IDE suggested including several classes - which I did. But after that I still got an error indication, namely the one you see above.
代码有什么问题?如何检索ID为ID的文档的priceInfo数组?
What's wrong with the code? How can I retrieve the priceInfo array of a Document with ID id?
********************************更新************** ********************
********************************UPDATE**********************************
根据请求,这是整个课程:
As per request, here's the whole class:
package DatabaseAccess;
import Models.GasStation;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import static com.mongodb.client.model.Filters.eq;
import static com.mongodb.client.model.Projections.excludeId;
import static com.mongodb.client.model.Projections.fields;
import static com.mongodb.client.model.Projections.include;
import com.mongodb.client.model.Updates;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.logging.Level;
import org.bson.Document;
public class databaseAccess {
private final String DB_HOST = "localhost";
private final int DB_PORT = 27017;
private final String DB_NAME = "db1";
private final String DB_COLLECTION = "prices";
private final MongoClient mongoClient;
private final MongoDatabase database;
private final MongoCollection<Document> collection;
public databaseAccess(){
mongoClient = new MongoClient(DB_HOST, DB_PORT);
database = mongoClient.getDatabase(DB_NAME);
collection = database.getCollection(DB_COLLECTION);
}
public String readFromDB(String id){
collection.find(eq("id", id)).first().projection(fields(include("priceInfo"), excludeId()));
return null;
}
}
推荐答案
您正在对方法中的调用链进行操作. 让我们分析链中的每个元素:
You're operating on chain of calls in your method. Let's analyze each element in chain:
MongoCollection:
MongoCollection:
FindIterable< TDocument> find()-查找集合中的所有文档.
FindIterable< TDocument> find() - Finds all documents in the collection.
返回类型为FindIterable<TDocument>
,您在其上调用链中的下一个方法:
Return type is FindIterable<TDocument>
and you calling the next method in chain on it:
FindIterable< TDocument>
FindIterable< TDocument>
从com.mongodb.async.client.MongoIterable接口继承的方法:
Methods inherited from interface com.mongodb.async.client.MongoIterable:
batchCursor,首先是forEach,进入地图
batchCursor, first, forEach, into, map
好的,我们要去MongoIterable
:
MongoIterable< TResult>:
MongoIterable< TResult>:
void first(SingleResultCallback回调)-帮助程序返回迭代器中的第一项或为null.
void first(SingleResultCallback callback) - Helper to return the first item in the iterator or null.
这意味着first(...)
不返回任何内容.您是从零开始调用projection(...)
的,当然这是不适用的,因此编译器将其标记为错误.
That means first(...)
is returned nothing. You're calling projection(...)
from nothing, of course this is not applicable, so the compiler marks this as an error.
要调用projection(Bson projection)
,您应该拥有FindIterable<T>
实例. MongoCollection.find()
可以为您提供此实例:
For calling projection(Bson projection)
you shoud have FindIterable<T>
instance. MongoCollection.find()
can provide you with this instance:
collection.find(eq("id", id)).projection(fields(include("priceInfo"), excludeId()));
这篇关于使用带有投影的find()方法使用mongodb java driver 3.4检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!