使用find() - 使用投影的方法使用mongodb java驱动程序3.4检索数据

时间:2022-10-23 15:42:47

I am using mongodb java driver 3.4.

我正在使用mongodb java驱动程序3.4。

In the mongodb database documents are saved according to the following structure:

在mongodb数据库中,文档按照以下结构保存:

{
    "_id" : ObjectId("595a9fc4fe3f36402b7edf0e"),
    "id" : "123",
    "priceInfo" : [
        {object1: value1}, {object2: value2}, {object3: value3}
    ]
}

In order to retrieve the "priceInfo"-Array of a Document with a specific id, I wrote the following code:

为了检索具有特定id的文档的“priceInfo”--Array,我编写了以下代码:

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

The problem is that my IDE won't accept this code.

问题是我的IDE不接受此代码。

It's giving me the following error indication:

它给了我以下错误指示:

使用find() - 使用投影的方法使用mongodb java驱动程序3.4检索数据

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.

我不知道为什么这段代码不起作用。起初IDE建议包括几个类 - 我做了。但在那之后我仍然有一个错误指示,即你在上面看到的那个。

What's wrong with the code? How can I retrieve the priceInfo array of a Document with ID id?

代码有什么问题?如何检索ID为ID的Document的priceInfo数组?

********************************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;     
    }

}

1 个解决方案

#1


2  

You're operating on chain of calls in your method. Let's analyze each element in chain:

您正在使用方法中的一系列调用进行操作。让我们分析链中的每个元素:

MongoCollection:

FindIterable< TDocument> find() - Finds all documents in the collection.

FindIterable find() - 查找集合中的所有文档。

Return type is FindIterable<TDocument> and you calling the next method in chain on it:

返回类型是FindIterable 并且您在链中调用下一个方法:

FindIterable< TDocument>

Methods inherited from interface com.mongodb.async.client.MongoIterable:

从com.mongodb.async.client.MongoIterable接口继承的方法:

batchCursor, first, forEach, into, map

batchCursor,first,forEach,into,map

Okay, we are going to MongoIterable:

好的,我们要去MongoIterable:

MongoIterable< TResult>:

void first(SingleResultCallback callback) - Helper to return the first item in the iterator or null.

void first(SingleResultCallback callback) - Helper返回迭代器中的第一个项或null。

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(...),当然这不适用,因此编译器会将此标记为错误。

For calling projection(Bson projection) you shoud have FindIterable<T> instance. MongoCollection.find() can provide you with this instance:

对于调用投影(Bson投影),你应该有FindIterable 实例。 MongoCollection.find()可以为您提供此实例:

collection.find(eq("id", id)).projection(fields(include("priceInfo"), excludeId()));

#1


2  

You're operating on chain of calls in your method. Let's analyze each element in chain:

您正在使用方法中的一系列调用进行操作。让我们分析链中的每个元素:

MongoCollection:

FindIterable< TDocument> find() - Finds all documents in the collection.

FindIterable find() - 查找集合中的所有文档。

Return type is FindIterable<TDocument> and you calling the next method in chain on it:

返回类型是FindIterable 并且您在链中调用下一个方法:

FindIterable< TDocument>

Methods inherited from interface com.mongodb.async.client.MongoIterable:

从com.mongodb.async.client.MongoIterable接口继承的方法:

batchCursor, first, forEach, into, map

batchCursor,first,forEach,into,map

Okay, we are going to MongoIterable:

好的,我们要去MongoIterable:

MongoIterable< TResult>:

void first(SingleResultCallback callback) - Helper to return the first item in the iterator or null.

void first(SingleResultCallback callback) - Helper返回迭代器中的第一个项或null。

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(...),当然这不适用,因此编译器会将此标记为错误。

For calling projection(Bson projection) you shoud have FindIterable<T> instance. MongoCollection.find() can provide you with this instance:

对于调用投影(Bson投影),你应该有FindIterable 实例。 MongoCollection.find()可以为您提供此实例:

collection.find(eq("id", id)).projection(fields(include("priceInfo"), excludeId()));