Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can't do this in one step with your current schema. In order for you to do this, you need to store the question IDs with each tag instead of what you have now. Generally, in MongoDB you store the relations between collections "the other way around" compared to a relational database. For example, you can store it as:</p> <p><strong>Tags</strong></p> <pre><code>{ "tagname": "tag1", "tagid": "id1", "questions" : [ 1, 3 ] } { "tagname": "tag2", "tagid": "id2" } </code></pre> <p><strong>Questions</strong></p> <pre><code>{ "questionid": 1. "title": "question title" } { "questionid": 2, "title": "question title" } { "questionid": 3, "title": "question title" } </code></pre> <p>Or perhaps <strong>also</strong> with the tags:</p> <pre><code>{ "questionid": 1. "title": "question title", tags: [ "id1" ] } { "questionid": 2, "title": "question title" } { "questionid": 3, "title": "question title", tags: [ "id1" ] } </code></pre> <p>Which variant you pick depends a bit on your data needs. Store tags with their questions, <strong>and</strong> questions with their tags uses more storage and also causes more work if you need to update tags/question, but it does make queries easier. For example, you can easily ask for all tags without questions after the above modifications with:</p> <pre><code>db.tags.find( { questions: { $exists: true } } ); </code></pre> <p>To do your query in Java, I suggest you have a look at <a href="http://docs.mongodb.org/ecosystem/tutorial/getting-started-with-java-driver/" rel="nofollow">http://docs.mongodb.org/ecosystem/tutorial/getting-started-with-java-driver/</a> which has lots of examples. A bit further down is: <a href="http://docs.mongodb.org/ecosystem/tutorial/getting-started-with-java-driver/#getting-a-set-of-documents-with-a-query" rel="nofollow">http://docs.mongodb.org/ecosystem/tutorial/getting-started-with-java-driver/#getting-a-set-of-documents-with-a-query</a> which indicates that you could do the above query like:</p> <pre><code>query = new BasicDBObject("questions", new BasicDBObject("$exists", true)); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload