Save Subset of MongoDB Collection to Another Collection

后端 未结 6 759
名媛妹妹
名媛妹妹 2020-11-30 22:37

I have a set like so

{date: 20120101}
{date: 20120103}
{date: 20120104}
{date: 20120005}
{date: 20120105}

How do I save a subset of those d

6条回答
  •  猫巷女王i
    2020-11-30 23:33

    There's no direct equivalent of SQL's insert into ... select from ....

    You have to take care of it yourself. Fetch documents of interest and save them to another collection.

    You can do it in the shell, but I'd use a small external script in Ruby. Something like this:

    require 'mongo'
    
    db = Mongo::Connection.new.db('mydb')
    
    source = db.collection('source_collection')
    target = db.collection('target_collection')
    
    source.find(date: "20120105").each do |doc|
      target.insert doc
    end
    

提交回复
热议问题