java mongodb3.2 find,使用带有投影的find()方法使用mongodb java driver 3.4检索数据

I am using mongodb java driver 3.4.

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

{

"_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:

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

I wrote this code according too the documentation, which you can find here:

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

It's giving me the following error indication:

lsSE5.png

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.

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 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:

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

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

FindIterable< TDocument>

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

batchCursor, first, forEach, into, map

Okay, we are going to MongoIterable:

MongoIterable< TResult>:

void first(SingleResultCallback callback) - Helper to return the first item in the iterator or 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.

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. MongoDB安装与配置 步骤如下: 1. 下载MongoDB: 在MongoDB官网 https://www.mongodb.com/ 中下载相应的版本。 2. 安装MongoDB: 运行下载后的安装程序,按照提示进行安装。 3. 配置MongoDB: 打开MongoDB安装目录下的bin文件夹,找到mongod.exe文件,将其添加到系统环境变量中,然后在命令行中输入mongod,启动MongoDB。 4. 测试MongoDB: 在命令行中输入mongo,连接MongoDB数据库。 2. MongoDB数据操作 MongoDB数据操作主要包括增删改查四个方面,具体操作如下: 1. 插入数据使用insert()方法插入数据,语法如下: db.collection.insert(document) 其中,db.collection是集合名称,document是要插入的数据。 2. 删除数据使用remove()方法删除数据,语法如下: db.collection.remove(query) 其中,db.collection是集合名称,query是删除条件。 3. 更新数据使用update()方法更新数据,语法如下: db.collection.update(query,update,options) 其中,db.collection是集合名称,query是更新条件,update是更新内容,options是更新选项。 4. 查询数据使用find()方法查询数据,语法如下: db.collection.find(query) 其中,db.collection是集合名称,query是查询条件。 3. Java使用MongoDB Java使用MongoDB需要引入MongoDB驱动程序,然后通过Java代码操作MongoDB数据库。 步骤如下: 1. 引入MongoDB驱动程序:在Java项目中引入MongoDB驱动程序,可以通过Maven或手动下载并添加到项目中。 2. 连接MongoDB使用MongoClient类连接MongoDB数据库,语法如下: MongoClient mongoClient = new MongoClient("localhost", 27017); 其中,localhost是MongoDB数据库所在的主机名,27017是MongoDB数据库所使用的端口号。 3. 获取MongoDB数据库:使用getDatabase()方法获取MongoDB数据库,语法如下: MongoDatabase mongoDatabase = mongoClient.getDatabase("test"); 其中,test是MongoDB数据库的名称。 4. 获取MongoDB集合:使用getCollection()方法获取MongoDB集合,语法如下: MongoCollection<Document> collection = mongoDatabase.getCollection("users"); 其中,users是MongoDB集合的名称。 5. 插入数据使用insertOne()方法插入数据,语法如下: Document document = new Document("name", "Tom").append("age", 18); collection.insertOne(document); 其中,name和age是数据的字段名,"Tom"和18是数据的值。 6. 删除数据使用deleteOne()方法删除数据,语法如下: collection.deleteOne(Filters.eq("name", "Tom")); 其中,name是数据的字段名,"Tom"是数据的值。 7. 更新数据使用updateOne()方法更新数据,语法如下: collection.updateOne(Filters.eq("name", "Tom"), new Document("$set", new Document("age", 20))); 其中,name是数据的字段名,"Tom"是数据的值,$set是更新操作符,age是更新后的值。 8. 查询数据使用find()方法查询数据,语法如下: MongoCursor<Document> cursor = collection.find().iterator(); while (cursor.hasNext()) { Document document = cursor.next(); System.out.println(document); } 其中,find()方法可以不带参数,表示查询所有数据。查询结果会返回一个游标,可以通过循环遍历游标获取查询结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值