如何从mongoDB中的两个不同集合中获取数据

时间:2015-06-29 08:01:02

标签: mongodb mongodb-query mongodb-java

我有两个不同的系列:

userimage:

> db.userimage.find()
{ "_id" : ObjectId("5590e67ee06eeab5ff79cdbf"), "transactionid" : 200, "age" : 30 }
{ "_id" : ObjectId("5590e730e06eeab5ff79cdc0"), "transactionid" : 100, "age" : 28 }
{ "_id" : ObjectId("5590e737e06eeab5ff79cdc1"), "transactionid" : 300, "age" : 29 }
{ "_id" : ObjectId("5590e741e06eeab5ff79cdc2"), "transactionid" : 400, "age" : 32 }  

usertest

> db.usertest.find()
{ "_id" : 100, "name" : "A" }
{ "_id" : 200, "name" : "B" }
{ "_id" : 300, "name" : "C" }
{ "_id" : 400, "name" : "D" }  

这里userimage的transactionid被归结为usertest集合的_id。我如何引用这两个集合.. 有没有更好的方法来设计这两个集合...
由于某些原因,我不想将这两个集合保留在一个集合中。

我想使用java代码:

以下是我要做的事情::

MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
        DB db = mongoClient.getDB( "ITAU" );
        userimage = db.getCollection("userimage");
        usertest = db.getCollection("usertest");
        BasicDBObject post = new BasicDBObject("transactionid", 200);
        DBCursor myCur = userimage.find(post);
        while(myCur.hasNext()) {
            System.out.println(myCur.next());

        }

如何同时获取收集数据......

2 个答案:

答案 0 :(得分:2)

您正在使用NoSQL数据库而不是RDBMS。 MongoDB不支持联接。因此,最好是合并集合并更改模式,如:

userimage收藏:

{ "_id" : ObjectId("5590e67ee06eeab5ff79cdbf"), "transactionid" : 200, "age" : 30 }

usertest收藏:

{ "_id" : 200, "name" : "B" }

最好使用 -

user集合:

{ "_id" : ObjectId("5590e67ee06eeab5ff79cdbf"), "transactionid" : 200, "age" : 30, "name" : "B" }

在这里,我假设一对一的关系。如果有一对多,您可以保留一个列表。

有关详细信息,请访问refer

答案 1 :(得分:1)

usertest集合name字段中不需要添加name字段in userimage collection so userimage`集合如下所示:

{ "_id" : ObjectId("5590e67ee06eeab5ff79cdbf"), "transactionid" : 200, "age" : 30 ,"name":"B" }
{ "_id" : ObjectId("5590e730e06eeab5ff79cdc0"), "transactionid" : 100, "age" : 28 ,"name":"A" }
{ "_id" : ObjectId("5590e737e06eeab5ff79cdc1"), "transactionid" : 300, "age" : 29 ,"name":"C" }
{ "_id" : ObjectId("5590e741e06eeab5ff79cdc2"), "transactionid" : 400, "age" : 32 ,"name":"D" }  

然后将您的代码用作:

    MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
    DB db = mongoClient.getDB( "ITAU" );
    userimage = db.getCollection("userimage");
    usertest = db.getCollection("usertest");
    BasicDBObject post = new BasicDBObject("transactionid", 200);
    DBCursor myCur = userimage.find(post);
    while(myCur.hasNext()) {
        System.out.println(myCur.next());
    }

返回给定条件的完整匹配文档。如果无法在name集合中移动userimage,那么您应该执行两个单独的while循环:

    MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
    DB db = mongoClient.getDB( "ITAU" );
    userimage = db.getCollection("userimage");
    usertest = db.getCollection("usertest");
    BasicDBObject post = new BasicDBObject("transactionid", 200);
    BasicDBObject _idPost = new BasicDBObject("_id", 200); // because _id = transactionid
    DBCursor myCur = userimage.find(post);
    DBCursor myCur1 = usertest.find(_idPost);
    while(myCur.hasNext()) {
        System.out.println(myCur.next());
    }
    while(myCur1.hasNext()) {
        System.out.println(myCur1.next());
    }