Query to get all posts of topics



  • Hello, please help me with mongo query to get all posts by topic id


  • GNU/Linux Gamers

    Do you actually need to do this via mongo?

    Could you make use of the API which would return it all as JSON.

    Every route in NodeBB has a read-only API accessible by prepending the route with /api

    If you want to POST to NodeBB, you'll want to look at the write API, which has its own documentation


  • Admin Staff

    const topicId = 5;
    const keys = db.objects.find({ _key: "tid:" + topicId + ":posts" }).map(i => 'post:' + i.value);
    printjson(db.objects.find({ _key: { $in: keys } }).toArray());
    

    You can save this in a file called query.js and then execute it with mongo localhost:27017/mydb query.js

    It will output something like

    [
            {
                    "_id" : ObjectId("5547ae6a65190fe21225ec2b"),
                    "_key" : "post:124",
                    "pid" : 124,
                    "uid" : 47,
                    "tid" : 5,
                    "content" : "I like it too :)",
                    "timestamp" : 1376791225638,
                    "bookmarks" : 2
            },
            {
                    "_id" : ObjectId("5547aee765190fe212296f10"),
                    "_key" : "post:18",
                    "pid" : 18,
                    "uid" : 2,
                    "tid" : 5,
                    "content" : "Thank you :)",
                    "timestamp" : 1373686442781
            }
    ]
    

Log in to reply
 

Suggested Topics

  • 10
  • 3
  • 11
  • 6
  • 1
| |