Я пытаюсь получить поле из моей коллекции mongodb.Моя БД имеет следующую структуру:
{ "_id" : "1", "username" : "", "password" : "!", "firm" : "SpringSource", "roles" : [ "admin", "client" ], "items" : [ { "_id" : "72d2ab6f-2f2a-4eb2-bb2a-86f0ee113e0a", "id" : "1", "title" : "Lugo", "price" : 10, "category" : "Coffee", "images" : "https://res.cloudinary.com/dznqctqoe/image/upload/v1550652877/ristretto-bianco.jpg", "description" : "Double shot with cream" }, { "_id" : "104ce91d-c80d-4c7f-a8e7-e1166ca945d8", "id" : "2", "title" : "Ristretto", "price" : 10, "category" : "Coffee", "images" : "https://res.cloudinary.com/dznqctqoe/image/upload/v1550652877/ristretto-bianco.jpg", "description" : "Double shot with milk" }, { "_id" : "3f7b52f5-133c-4a4c-b0b3-e22a97c32e5e", "id" : "3", "title" : "Black Tea", "price" : 5, "category" : "Tea", "images" : "https://res.cloudinary.com/dznqctqoe/image/upload/v1550652877/ristretto-bianco.jpg", "description" : "Black Tea" }, { "_id" : "17b9867a-9813-44b4-800d-323d04cc49ec", "id" : "4", "title" : "Green Tea", "price" : 7, "category" : "Tea", "images" : "https://res.cloudinary.com/dznqctqoe/image/upload/v1550652877/ristretto-bianco.jpg", "description" : "Green Tea" } ], "latitude" : "39.877619", "longitude" : "32.682537" }
Поэтому я пытаюсь получить только поля "id", "title", "price", "category" "images" "description". Но с помощью метода проекции я получаю поле _id с тегом items, как показано ниже:
[
{
"items": [
{
"_id": "72d2ab6f-2f2a-4eb2-bb2a-86f0ee113e0a",
"id": "1",
"title": "Lugo",
"price": 10,
"category": "Coffee",
"images": "https://res.cloudinary.com/dznqctqoe/image/upload/v1550652877/ristretto-bianco.jpg",
"description": "Double shot with cream"
},
Мой код Java такой:
MongoDBSingleton mongoDB = MongoDBSingleton.getInstance();
MongoDatabase db = mongoDB.getDatabase();
BasicDBObject query= new BasicDBObject("_id","1");
//query.put("items",new BasicDBObject("$exists",true));
List<Document> results = db.getCollection("users").find(query).projection(fields(include("items","id","title","price","category","image","description"),exclude("_id")))
.into(new ArrayList<Document>());
Я указал то, что мне нужно выше.Без тега Items, как получить результат JSON, как показано ниже:
{
id: 2,
title: "Smartphone 2",
price: "173000",
category: "smartphones",
images: [
"https://res.cloudinary.com/bogdan-fullstack-portfolio/image/upload/v1539481629/2.jpg"
],
description:
"dimentum gravida. Ut euismod eleifend velit at rhoncus. Cras et odio eget enim luctus gravida. Mauris ultricies eros tristique, aliquet. "
},