Migrate a NodeBB forum with MongoDB to Discourse

Somehow I managed to get the script function properly.

Here is my changes (with a little help from a Claude Code :slight_smile:)

 >>mongo.rb
   
   def posts(offset = 0, page_size = 1000)
      post_keys = mongo.find(_key: "posts:pid").skip(offset).limit(page_size).pluck(:value)
      post_keys
          .map { |pid| post(pid) }
          .compact  # <-- drops any nil results (orphaned pids)
      post_keys.map { |post_key| post(post_key) }
    end

    def post(id)
    post = mongo.find(_key: "post:#{id}").first
    return nil if post.nil? # <-- null checking
    post["timestamp"] = timestamp_to_date(post["timestamp"])
    if post["upvoted_by"] = mongo.find(_key: "pid:#{id}:upvote").first
        post["upvoted_by"] = mongo.find(_key: "pid:#{id}:upvote").first[:members]
      else
        post["upvoted_by"] = []
      end

      post["pid"] = post["pid"].to_s
      post["deleted"] = post["deleted"].to_s

      post
    end
	
>>nodebb.rb

 create_posts(posts, total: post_count, offset: offset) do |post|
        # skip if post is null
		# skip if it's merged_post
        next if post.nil?
        next if @merged_posts_map[post["pid"]]

        # skip if it's deleted
        next if post["deleted"] == "1"

        raw = post["content"]
        post_id = "p#{post["pid"]}"

        next if raw.blank?
        topic = topic_lookup_from_imported_post_id("t#{post["tid"]}")

        unless topic
          puts "Topic with id #{post["tid"]} not found, skipping"
          next
        end	

It seems working the right way now.

Although I don’t know how correct this is from the point of view of Discourse’s internal architecture, at first glance it seems to work.

Any suggestions for improvement and optimization are most welcome.

1 Like