sql >> Database >  >> NoSQL >> MongoDB

MongoDB-aggregatiequery om te tellen

U kunt onderstaande aggregatie gebruiken

db.users.aggregate([
  { "$match": { "_id": 2 }},
  { "$lookup": {
    "from": "follows",
    "let": { "id": "$_id" },
    "pipeline": [
      { "$facet": {
        "userFollows": [
          { "$match": { "$expr": { "$eq": ["$follower", "$$id"] }}}
        ],
        "myFollows": [
          { "$match": { "$expr": { "$eq": ["$follower", 1] }}}
        ]
      }},
      { "$project": {
        "matchedFollowed": {
          "$setIntersection": ["$userFollows.followed", "$myFollows.followed"]
        }
      }},
      { "$unwind": "$matchedFollowed" }
    ],
    "as": "user"
  }},
  { "$lookup": {
    "from": "follows",
    "let": { "ids": "$user.matchedFollowed" },
    "pipeline": [
      { "$match": { "$expr": { "$in": ["$follower", "$$ids"] }}}
    ],
    "as": "mutualConnections"
  }}
])



  1. kan geen standaard mongo-shellscript maken met authenticatie

  2. Wordt MongoDB _id (ObjectId) gegenereerd in oplopende volgorde?

  3. Kan ik rs.initiate() en rs.Add() aanroepen vanuit node.js met het MongoDb-stuurprogramma?

  4. MongoDB C#-stuurprogramma 'Cursor niet gevonden'