- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要将 mongodb 查询转换为 java。我有“对话” Collection 。Mongo db 以这种方式查询并且可以工作。
{
messages: { source: 1, $elemMatch: { date: { $gte: ISODate("2013-07-25 00:00:00"), $lt: ISODate("2013-08-26 00:00:00")}}}
}
我正在尝试 java BasicDBObject
elemMatch = new BasicDBObject();
BasicDBObject retrievedField = new BasicDBObject();
elemMatch.put("date", new BasicDBObject("$gte",StartDate).append("$lte",EndDate));
elemMatch.put("source", 1);
BasicDBObject up = new BasicDBObject();
up.put("$elemMatch",elemMatch);
retrievedField.put("messages", up);
DBCursor cursor = this.dbCollection.find( retrievedField).limit(10);
但是这段代码不起作用
我的 Collection 数据是
{
"_id" : ObjectId("51f130d6e4b0bf50e9bfc038"),
"saleId" : 2.43564e+07,
"saleCode" : "905155440001",
"randomId" : 9630,
"creatorId" : 8.21048e+06,
"recipientId" : 4.83831e+06,
"creatorType" : 1,
"returnReasonId" : 11,
"conversationClosed" : false,
"operatorCalled" : false,
"sellerNotified" : false,
"buyerNotified" : false,
"operatorCalledDate" : null,
"creationDate" : ISODate("2013-07-25T14:06:14.967Z"),
"lastUpdateDate" : ISODate("2013-08-15T08:46:10.115Z"),
"messages" : [
{
"senderId" : 8.21048e+06,
"source" : 1,
"seenByBuyer" : true,
"seenBySeller" : true,
"seenByOperator" : true,
"date" : ISODate("2013-07-25T14:06:39.968Z"),
"messageBody" : "asdad"
},
{
"senderId" : 8.21048e+06,
"source" : 1,
"seenByBuyer" : true,
"seenBySeller" : true,
"seenByOperator" : true,
"date" : ISODate("2013-07-25T14:06:59.978Z"),
"messageBody" : "asdasdawdwa"
},
{
"senderId" : 0,
"source" : 4,
"seenByBuyer" : true,
"seenBySeller" : true,
"seenByOperator" : true,
"date" : ISODate("2013-07-25T14:07:20.044Z"),
"messageBody" : "ad"
}
]
}
有什么帮助吗?
Calendar calStartDate = Calendar.getInstance();
Calendar calEndDate = Calendar.getInstance();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
Date StartDate = simpleDateFormat.parse(messageStartDate);
Date EndDate = simpleDateFormat.parse(messageEndDate);
calStartDate.setTime(StartDate);
calEndDate.setTime(EndDate);
Date sdate = new DateTime(calStartDate.get(Calendar.YEAR),calStartDate.get(Calendar.MONTH), calStartDate.get(Calendar.DAY_OF_MONTH),calStartDate.get(Calendar.HOUR),calStartDate.get(Calendar.MINUTE), DateTimeZone.UTC).toDate();
Date edate = new DateTime(calEndDate.get(Calendar.YEAR), calEndDate.get(Calendar.MONTH), calEndDate.get(Calendar.DAY_OF_MONTH), calEndDate.get(Calendar.HOUR), calEndDate.get(Calendar.MINUTE), DateTimeZone.UTC).toDate();
BasicDBObject query = new BasicDBObject(
"messages",
new BasicDBObject("$elemMatch",new BasicDBObject("source", 1).append("date", new BasicDBObject("$gte", sdate).append("$lt", edate)))
);
DBCursor cursor = this.dbCollection.find( query).limit(10);
最佳答案
无论如何,您的 shell 查询都不正确。应该是:
{
"messages": {
"$elemMatch": {
"source": 1,
"date": {
"$gte": ISODate("2013-07-25 00:00:00"),
"$lt": ISODate("2013-07-26 00:00:00")
}
}
}
}
遵循相同的嵌套规则会有所帮助,这样您就不会出现错误的流程:
BasicDBObject query = new BasicDBObject(
"messages",
new BasicDBObject(
"$elemMatch",
new BasicDBObject( "source", 1 )
.append( new BasicDBObject(
"date",
new BasicDBObject( "$gte", StartDate )
.append( "$lt", EndDate )
)
)
)
);
确保“日期”的类型为 java.util.Date
从 jodatime 之类的东西返回也对序列化有效。并确保您构建的时间是 UTC 时间。
示例:
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
Date StartDate = new DateTime(2013, 7, 25, 0, 0, DateTimeZone.UTC).toDate();
关于java - 将 MongoDb 查询转换为 Java BasicDbObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31334808/
case 3: BasicDBObject []document= new BasicDBObject(); // error DBCollection tab
我有一个实体 ProjectCycle映射到 mongo 数据库集合 ProjectCycle .我正在尝试检索 2 个字段,_id和 Status .我能够像下面这样检索两者 @Document(c
在下面的代码中,我尝试按 BasicDBObject 列表的日期降序自定义排序。 打印的输出是: { "year" : "2015" , "month" : "12" , "day" : "1" ,
我的 api java mongodb 有一个大问题。我使用带有 DBCollection 类的更新方法的请求,在 mongodb 中我得到多个相同的文档,但值没有改变,请帮助我。我不想有重复的文档。
我正在使用 MongoDB 和 Java 驱动程序开发 Java 应用程序。 我需要将一个BasicDBObject解析为我代码自己的对象,不知道是否有办法自动开发。 是否可以从 BasicDBObj
我正在为 mongo-db 使用 Java 驱动程序,并尝试使用 BasicDBObjectBuilder 添加多个查询条件。我有一个文本字段,其中 XML 存储为字符串,因此我们使用正则表达式来形成
我正在尝试使用 Morphia 将 HashMap 嵌入到我的 MongoDB 中。在与此处的帖子相同的警告之后: Morphia - Class not found defined in dbObj
我正在开发简单的 Web 应用程序来学习 jsp、mongodb、html。我在 jsp 中创建了一个简单的注册表单,它从用户那里获取 Name、Address 和 MobileNo 信息并将其插入到
我无法将值 30 添加到键年龄,我收到错误消息:BasicBSONObject 类型中的 put(String, Object) 方法不适用于参数 (String, int)。也适用于 newDate
我从 MongoDB 游标返回数据 BasicDBObject ,其 toString() 返回以下数据: { "_id" : { "$oid" : "52b8775ebf552f39c2ae
我在 mongodb 数据库中有以下 gridfs: db.outputFs.files.find() { "_id" : ObjectId("000000000000000000000001"),
我想将下面的json结构转换成java中的BasicDBOject并插入到mongo db中。 我的 JSON 结构是 { "it": { "batch": "2013",
我正在使用 MongoDB v3.2.0 和 Mongo Java Driver 3.0.4 版本。我正在使用 BasicDBObject(已弃用)而不是在 java 中使用 Document,因为我
我需要将 mongodb 查询转换为 java。我有“对话” Collection 。Mongo db 以这种方式查询并且可以工作。 { messages: { source: 1, $ele
我有一个 String 和一个包含列表的 JSON。它是这样的: { "counts":[ {"foo827138": 123124, "bar2918":312312, "s
我一直在开发一个 Java 程序,该程序使用 Mongo 数据库来存储基于地理坐标的飞机的某些数据。该应用程序正在收集信息并以正确的方式填充模型对象。 当我尝试将对象发送到 mongo 数据库时,出现
我需要将序列化的 Java 对象(从 Redis 获取)转换为扩展 com.mongodb.BasicDBObject (mongo-java-driver) 的 Java 模型。 class Mem
我无法从 MongoDB 中的查询文档中检索值。 比如doc结构是这样的: { "_id": { "$oid": "50f93b74f9eccc540b
我正在使用mongo db driver 2.11.2 。我有点困惑如何向 BasicDBObject 插入/添加数组。我遇到的所有示例都没有显示如何实现此目的:(。在下面的示例中,我将如何在 dbo
我有以下在 mongo shell 中执行的 mongo 查询。 db.test.update({ uuid: "160597270101684", sessionId: "1605972701016
我是一名优秀的程序员,十分优秀!