Wednesday 22 August 2018

get-nested-array-document-with-filter-mongo-db


Consider the Schema :

 "data": {
        "chapters": {
            "_id": "5b7c023bff64339c40613161",
            "courseworkId": "5b7c0228ff64339c40613160",
            "chapterName": "chapter1",
            "subChapters": [
                {
                    "questions": [
                        {
                     
                            "_id": "5b7c0a26ff64339c40613166",
                            "title": "test"
                        },
                        {
                            "_id": "5b7d046ee07f6e9ee72d0e38",
                            "title": "question 2"
                       
                        }
   ],
 "_id": "5b7c0446ff64339c40613162",
 "subChapterName": "subChapter9"
 }
}



GET sub document that match question ID : 5b7c0a26ff64339c40613166


1. Match the results for chapter Id
2. Go through the subchapters and see if we match the question Id and filter the results
db.chapter.aggregate([
  { "$match": {
    "_id" : ObjectId("5b7c023bff64339c40613161")
  }},         
  { "$addFields": {
    "subChapters": {
      "$filter": {
        "input": {
          "$map": {
            "input": "$subChapters",
            "as": "sc",
            "in": {
              "_id": "$$sc._id",
              "questions": {
                "$filter": {
                  "input": "$$sc.questions",
                  "as": "qu",
                  "cond": {
                      "$eq": [ "$$qu._id", ObjectId("5b7c0a26ff64339c40613166")  ]
                  }
                }
              }           
            }
          },
        },
        "as": "sc",
        "cond": {
          "$and": [
            { "$gt": [ { "$size": "$$sc.questions" }, 0 ] }
          ]
        }
      }
    }
  }}
])

Tuesday 21 August 2018

git-merge-local-to-remote

git is basically nothing but pointers .

its just keep references to this pointers and play around with these branch and merge


So, when you want to merge your branch changes to your master .  you need to checkout to master
first and then do the merge of that branch.


> git branch <branch-name> 
           or
> git checkout -b <branch-name>

If you want to create a new branch to retain commits you create, you may

do so (now or later) by using -b with the checkout command again

> git add <some file > and git commit -m <somefile>

> git push origin <branch-name>

> git checkout master

> git merge <branch-name>

you may get conflicts . so , please try to resolve them and then push them to master or branch

> again git commit with removed conflicts -> Merge Success


Create local branch from remote and merge your changes.

> git checkout <origin/branchname>

> git checkout -b '<local branch name>'

> Do temporary changes or any commits to your local

> git checkout <origin/branchnam>

> git merge <localbranchname>

> git push origin <branchname>   ==> Merging to remote place .






git branch overview

* git branch -a

List down all the branch in the local and remote

* git branch -r

list down all the branch in remote



* git fetch --all

Fetches all the remote repos to local

* git checkout -b <branch name>

Checkout the branch name with current HEAD to the master.

Always do git fetch <remote> <branch> and git checkout -b <branch name> ==>


** git fetch + git merge (Safe to do instead git pull --> this is sometimes aggresive )==> git pull