[英]Spring Boot java with Mongodb
package com.javatechie.spring.mongo.api.resource;
import java.util.List;
import java.util.Optional;
import org.bson.Document;
import org.bson.json.JsonReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.QualifierAnnotationAutowireCandidateResolver;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.javatechie.spring.mongo.api.model.Book;
import com.javatechie.spring.mongo.api.repository.BookRepository;
@RestController
public class BookController {
@Autowired
private BookRepository repository;
@Autowired
private MongoTemplate mongoTemplate;
@PostMapping("/addBook")
public String saveBook(@RequestBody Book book) {
repository.save(book);
return "Added book with id : " + book.getId();
}
@GetMapping("/findAllBooks")
public List<Book> getBooks() {
String jsonCommand = "{ aggregate: 'Book', pipeline: [ { $project: { _id: 0, 'bookName': 1
} } ], cursor: { } }";
Document resultDoc = mongoTemplate.executeCommand(jsonCommand);
System.out.println(resultDoc);
return repository.findAll();
}
我可以成功地使用這個在控制台上打印一個特定的字段
String jsonCommand = "{ 聚合:'Book',管道:[ { $project: { _id: 0, 'bookName': 1 } } ],cursor: { } }";
但我想打印特定數據,例如在我的 bookName 字段中有很多書,所以我只想打印 Python 目前它正在打印所有 bookName。
請幫助我在這里我被困住了
我建議您將$match添加到您的聚合管道中。 像{$match:{ "bookName":"Python"}}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.