【问题标题】:Convert MongoDb query into Java BasicDbObject将 MongoDb 查询转换为 Java BasicDbObject
【发布时间】:2015-07-10 07:21:04
【问题描述】:

我需要将 mongodb 查询转换为 java。 我有“对话”系列。 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);

但是这段代码不起作用

我的收藏数据是

    {
    "_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);

【问题讨论】:

    标签: java mongodb mongodb-query mongodb-java


    【解决方案1】:

    您的 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();
    

    【讨论】:

    • SimpleDateFormat simpleDateFormat = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");日期开始日期 = simpleDateFormat.parse(messageStartDate); StartDate和endDate是这样的吗?
    • @neoerol 不,这些是字符串。你需要像我说的那样传入一个日期对象。 MongoDB 存储为“日期”BSON 类型而不是字符串。正如我在答案中所说,使用产生java.util.Date 的东西。
    • @neoerol 在答案末尾添加了一个简单的示例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-14
    • 1970-01-01
    • 2015-06-29
    • 1970-01-01
    • 2018-08-28
    • 2020-01-19
    • 1970-01-01
    相关资源
    最近更新 更多