如果我在mongoDB中保存了以下架构中的文档:
{
createdDate: Date,
lastUpdate: Date
}
Run Code Online (Sandbox Code Playgroud)
是否可以查询创建和上次更新之间的时间段是否大于一天的文档?
直到最新版本的 spring,我看到了很多堆栈溢出问题,这表明 spring-data-mongodb 中不支持此操作 新的 spring-data-mongodb 1.10.0 中是否支持此操作
db.orders.aggregate([
{
$lookup:
{
from: "inventory",
localField: "item",
foreignField: "sku",
as: "inventory_docs"
}
}
])
Run Code Online (Sandbox Code Playgroud) DB db = mongo.getDB("sample");
DBCollection table = db.getCollection("samplecollection");
DBCursor cursor2 = table.find();
Run Code Online (Sandbox Code Playgroud)
给出的结果为:{“ _id”:{“ $ oid”:“ 58bfbcff1d30d8a8c1194328”},“ ID”:1,“ NAME”:“ dgsdg”}
如何获取没有objectid的文档?
我最近开始使用SpringData探索MongoDB中的Aggregation Framework。我可以创建以下查询,即
db.consumer_order.aggregate([
{ $match: {_id: ObjectId("59e43f542397a00de0c688e4"), "orderState":"Confirmed"}},
{ $project: {
parts: {$filter: {
input: '$parts',
as: 'item',
cond: {$eq: ['$$item.currentState', "Estimation Confirmed"]}
}}
}}
])
Run Code Online (Sandbox Code Playgroud)
Spring在MongoDB Native Driver中使用以下代码
List<Document> aggrigationList = new ArrayList<>();
List<String> conditions = new ArrayList<>();
conditions.add("$$item.currentState");
conditions.add("Estimation Confirmed");
Document matchDoc = new Document("$match",new Document("_id",new ObjectId(orderId)));
Document projectDoc = new Document("$project",new Document("parts",new Document("$filter",new Document("input","$parts").append("as", "item").append("cond", new Document("$eq",conditions)))));
aggrigationList.add(matchDoc);
aggrigationList.add(projectDoc);
Document orderWithPendingParts = consumerOrderCollection.aggregate(aggrigationList).first();
Run Code Online (Sandbox Code Playgroud)
但我确实知道始终与本机驱动程序一起工作不是一个好习惯,因为我们已经掌握了Spring-Data。但是我无法使用Spring Data将上述MongoDB查询构造到AggrigationObject。我尝试了以下内容,但是在构造Aggregation.project()时遇到困难,即
mongoTemplate.aggregate(Aggregation.newAggregation(
Aggregation.match(Criteria.where("owner").is(user).andOperator(Criteria.where("orderState").is("confirmed"))),
***Finding Difficulty in here***
), inputType, outputType)
Run Code Online (Sandbox Code Playgroud)
指导我,如果我做错了什么。
我对 MongoDB 相当陌生,需要帮助根据另一个集合的数据对一个集合进行选择或某种左连接。
我有两个集合,动物和膳食,我想获取在某个日期(假设是 20171001)之后上次注册膳食的动物,以确定该动物是否仍然活跃。
collection animals:
{
name: mr floof,
id: 12345,
lastMeal: abcdef
},
{
name: pluto,
id: 6789,
lastMeal: ghijkl
}
collection meals:
{
id: abcdef,
created: 20171008,
name: carrots
},
{
id: ghijkl,
created: 20170918,
name: lettuce
}
Run Code Online (Sandbox Code Playgroud)
因此,在这种情况下查询的预期输出将是:
{
name: mr floof,
id: 12345,
lastMeal: abcdef
}
Run Code Online (Sandbox Code Playgroud)
由于 Floof 先生在 20171008 年(即 20171001 年之后)吃过最后一顿饭。
希望我说得足够清楚,但如果没有,请随时询问。
我有一个正常工作的查询,它只返回我的实体(Prl)的一个属性:
@Query("{'recibido' : null ,'activo' : true}")
public List<EmpleadoIdDTO> findIdsEmpleadosPrlActivoRecibidoIsNull();
Run Code Online (Sandbox Code Playgroud)
班级:
public class EmpleadoIdDTO {
private Long empleadoId;
public Long getEmpleadoId() {
return empleadoId;
}
public void setEmpleadoId(Long empleadoId) {
this.empleadoId = empleadoId;
}
}
Run Code Online (Sandbox Code Playgroud)
我需要将此查询传递给 Criteria,因为它会增长:
import java.util.ArrayList;
import java.util.List;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.query.BasicQuery;
import org.springframework.data.mongodb.core.query.Criteria;
import com.xxx.crm482.domain.Prl;
import com.xxxx.crm482.service.dto.FilterPrlDTO;
public class PrlRepositoryImpl implements PrlRepositoryCustom {
private final MongoOperations mongoOperations;
public PrlRepositoryImpl(MongoOperations mongoOperations) {
this.mongoOperations = mongoOperations;
}
@Override
public List<Prl> find(FilterPrlDTO filterPrlDTO) {
List<Criteria> andCriteria = …Run Code Online (Sandbox Code Playgroud) 我想知道在 MongoDB 中获取特定文档的相关文档计数的最佳实践是什么。
场景如下:我需要获取用户分享的帖子,我还需要获取与这些帖子相关的评论总数。
如果可能,我想使用 MongoDB 聚合方式(如果这是最好的方式)我知道如何使用单独的方法 .count() 和 .find() 执行此查询。
帖子集合中的文档:
{
_id: ObjectId('5a66321e7e2043078bc3b88a'),
uid: 'UniqueIDofTheUser',
text: 'Post text'
}
Run Code Online (Sandbox Code Playgroud)
评论集合中的文档
{
_id: ObjectId('5a66321e7e2043078bc3b88c'),
uid: 'UniqueIDofTheUser',
post_id: ObjectId('5a66321e7e2043078bc3b88a'),
text: 'Comment text'
}
Run Code Online (Sandbox Code Playgroud)
预期结果:
[
{
_id: ObjectId('5a66321e7e2043078bc3b88a'),
uid: 'UniqueIDofTheUser',
text: 'Post text',
commentsCount: 20
},
{
_id: ObjectId('5a66321e7e2043078bc3b88c'),
uid: 'UniqueIDofTheUser',
text: 'Another post',
commentsCount: 3
},
{
_id: ObjectId('5a6632e17e2043078bc3b88f'),
uid: 'UniqueIDofTheUser',
text: 'Some random post',
commentsCount: 4
},
]
Run Code Online (Sandbox Code Playgroud) 我正在寻找一位翻译来改变这一点:
getCollection('migrate').aggregate([
{ "$project": {
"Contrat": {"Field1":"$Field1", "Field2":"$Field2"},
"Formule": {"Field3":"$Field3", "Field4":"$Field4"}
}},
{ "$project": {
"Contrats": {"Contrat":"$Contrat", "Formule":"$Formule"}
}}
])
Run Code Online (Sandbox Code Playgroud)
MongoJava 聚合框架。就像是 :
AggregationOperation project = Aggregation.project("Field1,Field2"); // while naming it "Contrat"
AggregationOperation project2 = Aggregation.project("Field3,Fiel4"); // while naming it Formule
AggregationOperation project3 = Aggregation.project("Contrat,Formule"); // while naming it " Contrats"
AggregationOperation out = Aggregation.out("test");
Aggregation aggregation = Aggregation.newAggregation(project, project2, project3, out);
mongoTemplate.aggregate(aggregation, "<nameOfInitialCollection>", Class.class);
Run Code Online (Sandbox Code Playgroud)
我在文档中找不到我的答案,我认为文档太差了,或者我可能太迷失在其中了(|愚蠢)。
我会提前感谢你的。
我试图从用户表中获取关注者的数量,该表基本上是使用 java 编程的数组列表。
我正在使用下面的查询来使用命令行界面获取计数。
db.userinfo.aggregate([{ $project : {followersCount : {$size: "$followers"}}}])
Run Code Online (Sandbox Code Playgroud)
但我无法在 java 中创建与我新手相同的查询。下面是我用 java 编写的代码,我收到 com.mongodb.MongoCommandException: Command failed with error 17124: 'The argument to $size must be an array, but was of type: int' 错误。
AggregateIterable<Document> elements = collectionUserInfo.aggregate(
Arrays.asList(new BasicDBObject("$project", new BasicDBObject("followers",
new BasicDBObject("$size", new BasicDBObject("followers",1).put("followers",new BasicDBObject("$ifNull", "[]")))))));
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮我解决这个问题吗
我正在尝试使用RepositoryRestResource和实现一个rest apiRestTemplate
除了加载@DBRef 之外,一切都运行良好
考虑这个数据模型:
public class Order
{
@Id
String id;
@DBRef
Customer customer;
... other stuff
}
public class Customer
{
@Id
String id;
String name;
...
}
Run Code Online (Sandbox Code Playgroud)
以及以下存储库(客户的类似存储库)
@RepositoryRestResource(excerptProjection = OrderSummary.class)
public interface OrderRestRepository extends MongoRepositor<Order,String>{}
Run Code Online (Sandbox Code Playgroud)
其余 api 返回以下 JSON:
{
"id" : 4,
**other stuff**,
"_links" : {
"self" : {
"href" : "http://localhost:12345/api/orders/4"
},
"customer" : {
"href" : "http://localhost:12345/api/orders/4/customer"
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果由 resttemplate 正确加载,将创建一个新的 Order 实例,其中 customer = null
是否可以在存储库端急切地解决客户并嵌入 …