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

How do I get the data from two different collection in mongoDB

我有两个不同的 collection:

用户图片:

> 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 }  

用户测试

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

此处 userimage 的 transactionid 与 usertest 的 _id 有关 collection..我如何引用这两个 collection.. 有没有更好的方法来设计这两个 collection...
由于某种原因,我不想将 collection 都保留在一个 collection 中。

我想使用 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());

        }

我怎样才能同时获得 collection 数据...

您使用的是 NoSQL 数据库而不是 RDBMS。 MongoDB 不支持联接。因此,如果您可以合并 collection 并更改您的架构,那就更好了:

userimage collection:

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

usertest collection:

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

更好用-

user collection:

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

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

更多详情,请refer

usertest 集合 name 字段中不需要,然后添加 name 字段 inuserimagecollection souserimage` 集合如下所示:

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

它return 给定条件的全部匹配文档。如果无法移动 userimage 集合中的 name 文件,那么您应该像这样执行两个单独的 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());
    }