Product SiteDocumentation Site

Chapter 6. Collections

6.1. Collections Used Throughout This Book
Even though you can build your own JSON values with JSONiq by copying-and-pasting JSON documents, most of the time, your JSON data will be in a collection.
We now introduce collections, because collections are perfect to illustrate the JSON navigation syntax which will be introduced in the next section.
Collections are sequences of objects, identified by a name which is a string.
Adding or deleting collections from the set of known collections to a query processor, and loading the data in a collection are implementation-dependent and outside of the scope of this book.
We will just assume that there is a function named collection() that returns all objects associated with the provided collection name.
Example 6.1. Getting all objects from a collection.
collection("one-object")
Results:
{
  "question" : "What NoSQL technology should I use?"
}

6.1. Collections Used Throughout This Book

For illustrative purposes, we will assume that we have the following collections:
  • collection("one-object")
    Example 6.2. The object in the one-object collection.
    collection("one-object")
    
    Results:
    {
      "question" : "What NoSQL technology should I use?"
    }
    

  • collection("faqs") - this is a collection of StackOverflow FAQs.
    Example 6.3. One object from the faqs collection.
    collection("faqs")[1]
    
    Results:
    {
      "_id" : "511C7C5C9A277C22D138802D",
      "question_id" : 4419499,
      "last_edit_date" : "2012-12-17T00:02:31",
      "creation_date" : "2010-12-11T23:15:19",
      "last_activity_date" : "2012-12-17T00:02:31",
      "score" : 15,
      "accepted_answer_id" : 4421601,
      "title" : "MySQL and NoSQL: Help me to choose the right o
    ne",
      "tags" : [ "php", "mysql", "nosql", "cassandra" ],
      "view_count" : 3972,
      "owner" : {
        "user_id" : 279538,
        "display_name" : "cedivad",
        "reputation" : 430,
        "user_type" : "registered",
        "profile_image" : "http://www.gravatar.com/avatar/b77fa
    dd2ba791134ac40a9c184be1eda?d=identicon&r=PG",
        "link" : "http://stackoverflow.com/users/279538/cedivad
    ",
        "accept_rate" : 74
      },
      "link" : "http://stackoverflow.com/questions/4419499/mysq
    l-and-nosql-help-me-to-choose-the-right-one",
      "is_answered" : true
    }
    

  • collection("answers") - this is a collection of StackOverflow answers (to the previous FAQs).
    Example 6.4. One object from the answers collection.
    collection("answers")[1]
    
    Results:
    {
      "_id" : "511C7C5D9A277C22D13880C3",
      "question_id" : 37823,
      "answer_id" : 37841,
      "creation_date" : "2008-09-01T12:14:38",
      "last_activity_date" : "2008-09-01T12:14:38",
      "score" : 7,
      "is_accepted" : false,
      "owner" : {
        "user_id" : 2562,
        "display_name" : "Ubiguchi",
        "reputation" : 1871,
        "user_type" : "registered",
        "profile_image" : "http://www.gravatar.com/avatar/00b87
    a917ec763c0c051dc6b8c06f402?d=identicon&r=PG",
        "link" : "http://stackoverflow.com/users/2562/ubiguchi"
    
      }
    }
    

Many queries in this book can be directly input into 28.io's try-it-now sandbox, as these collections are preloaded (this is real-world data).