将mongo Shell脚本转换为java

时间:2016-02-18 12:27:52

标签: java mongodb

我需要使用BasicDBObject将以下Mongo Shell查询转换为Java查询。

db.transactions.find(
    {
        "$or":[
            { 
               "originUser._id": ObjectId("56c5b433fbea2f2b96e06148"),
               "targetUser._id": ObjectId("56c5b433fbea2f2b96e06149")
            },         
            {
               "originUser._id": ObjectId("56c5b433fbea2f2b96e06149"),
               "targetUser._id": ObjectId("56c5b433fbea2f2b96e06148")
            }
       ]
   }
);

我试过的是:

BasicDBObject clause1 = new BasicDBObject("originUser._id", userId).append("targetUser._id ", transactionQuery.getUserId());
BasicDBObject clause2 = new BasicDBObject("originUser._id", transactionQuery.getUserId()).append("targetUser._id ", userId);
BasicDBObject or_conditions[]={clause1,clause2};
query.append("$or", or_conditions);

结果显示:         对象1:

{
    "_id" : ObjectId("55b22649cd6a900b96bb040a"),
    "originUser" : {
        "_id" : ObjectId("56c5b433fbea2f2b96e06149"),
        "firstName" : "Mark",
        "lastName" : "Ti",
    },
    "targetUser" : {
        "_id" : ObjectId("56c5b433fbea2f2b96e06148"),
        "firstName" : "John",
        "lastName" : "Doe",
    },
}

对象2:

{
    "_id" : ObjectId("55b22649cd6a900b96bb040b"),
    "originType" : "NFC",
    "originUser" : {
        "_id" : ObjectId("56c5b433fbea2f2b96e06148"),
        "firstName" : "John",
        "lastName" : "Doe",
    },
    "targetUser" : {
        "_id" : ObjectId("56c5b433fbea2f2b96e06149"),
        "firstName" : "Mark",
        "lastName" : "Ti",
    },
}

java中的查询字符串:

{ "$or" : [ { "originUser._id" : { "$oid" : "56c5b433fbea2f2b96e06148"} , "targetUser._id " : { "$oid" : "56c5b433fbea2f2b96e06149"}} , { "originUser._id" : { "$oid" : "56c5b433fbea2f2b96e06149"} , "targetUser._id " : { "$oid" : "56c5b433fbea2f2b96e06148"}}]}

当我运行shell脚本的查询但是当我从java运行查询时没有返回任何结果时,会显示此结果。

请帮助

0 个答案:

没有答案