0

I need to know, can I make complex query which will return me result with array board but only with parameters Object inside :

  • board.name
  • _id
  • array with (lists.list and index position in array)
  • length cards in array

I'm weak in mongodb so I ask if it is possible to do so, because I have order and I do not know whether to do it in the query or on the loop in the angularjs

Here is from db result with query db.getCollection('boards').find({}) (2results)

    /* 1 */
{
    "_id" : ObjectId("5a0b5c6da0502e2174fd849f"),
    "name" : "example 1",
    "users" : [ 
        ObjectId("59cd114cea98d9326ca1c421")
    ],
    "lists" : [ 
        {
            "list" : "example",
            "_id" : ObjectId("5a0b5c7ba0502e2174fd84ae"),
            "cards" : [ 
                {
                    "name" : "1",
                    "_id" : ObjectId("5a0b5c80a0502e2174fd84b4")
                }, 
                {
                    "name" : "2",
                    "_id" : ObjectId("5a0b5c80a0502e2174fd84b3")
                }, 
                {
                    "name" : "3",
                    "_id" : ObjectId("5a0b5c80a0502e2174fd84b2")
                }
            ]
        }, 
        {
            "list" : "example 1",
            "_id" : ObjectId("5a0b5c7ba0502e2174fd84ad"),
            "cards" : [ 
                {
                    "name" : "1",
                    "_id" : ObjectId("5a0b5c83a0502e2174fd84b5") 
                }
            ]
        }, 
        {
            "list" : "example",
            "_id" : ObjectId("5a0b5c7ba0502e2174fd84ac"),
            "cards" : [ 
                {
                    "name" : "2",
                    "_id" : ObjectId("5a0b5c85a0502e2174fd84b6") 
                }
            ]
        }
    ], 
    "__v" : 0
}

/* 2 */
{
    "_id" : ObjectId("5a0b5c71a0502e2174fd84a4"),
    "name" : "example 2",
    "users" : [ 
        ObjectId("59cd114cea98d9326ca1c421")
    ],
    "lists" : [ 
        {
            "list" : "example next 1",
            "_id" : ObjectId("5a0b5c93a0502e2174fd84bc"),
            "cards" : [ 
                {
                    "name" : "1",
                    "_id" : ObjectId("5a0b5c95a0502e2174fd84c2")
                }, 
                {
                    "name" : "2",
                    "_id" : ObjectId("5a0b5c95a0502e2174fd84c1")
                }, 
                {
                    "name" : "3",
                    "_id" : ObjectId("5a0b5c95a0502e2174fd84c0")
                }
            ]
        }, 
        {
            "list" : "example next 2",
            "_id" : ObjectId("5a0b5c93a0502e2174fd84bb"),
            "cards" : [ 
                {
                    "name" : "1",
                    "_id" : ObjectId("5a0b5c98a0502e2174fd84c5")
                }, 
                {
                    "name" : "2",
                    "_id" : ObjectId("5a0b5c98a0502e2174fd84c4")
                }
            ]
        }, 
        {
            "list" : "example next 3",
            "_id" : ObjectId("5a0b5c93a0502e2174fd84ba"),
            "cards" : [ 
                {
                    "name" : "1",
                    "_id" : ObjectId("5a0b5c9aa0502e2174fd84c6")
                }
            ]
        }
    ], 
    "__v" : 0
}

1 Answers1

0

Yes,

You can structure your own response, with aggregation framework .

https://docs.mongodb.com/manual/aggregation/

The most useful stages in that framework I used are:

$project: Structure what I want.
$match: Very similiar to select in sql.
$group: to make groups
$sort: to sort :D
$unwind: to unwind arrays.

There are more I think but depend on how you want structure, An excelent tutorial here:

https://www.tutorialspoint.com/mongodb/mongodb_aggregation.htm

  • Hey thanks !, one more question is how can I add index position item list inside lists array ?? – Aneta Jabłońska Nov 14 '17 at 21:50
  • @AnetaJabłońska, Sorry I never tried that exactly. I don't know why you want it, but maybe you can solve your logic with $arrayElemAt: where you get an element in an array given its index. or $indexOfArray: Where you get the index of a specific element in an array. – Pablo Cesar Cordova Morales Nov 14 '17 at 22:17