dongqufi82315
2018-12-21 13:51
浏览 342

如何使用mongo-go-driver运行mongo命令?

Hi there :) I'm working on a golang app linked to mongo DB (I use the official driver: mongo-go) and here's my problem,I want to execute this function

db.rmTickets.find().forEach(function(doc) {
    doc.created=new Date(doc.created)
    doc.updated=new Date(doc.updated)
    doc.deadline=new Date(doc.deadline)
    doc.dateEstimationDelivery=new Date(doc.dateEstimationDelivery)
    doc.dateTransmitDemand=new Date(doc.dateTransmitDemand)
    doc.dateTransmitQuotation=new Date(doc.dateTransmitQuotation)
    doc.dateValidationQuotation=new Date(doc.dateValidationQuotation)
    doc.dateDeliveryCS=new Date(doc.dateDeliveryCS)
    db.rmTickets.save(doc)
})

I see on godoc that a Database.RunCommand() exists but I'm not sure about how to use it. If someone can help :) Thanks

图片转代码服务由CSDN问答提供 功能建议

您好:)我正在研究链接到mongo DB的golang应用程序(我使用官方驱动程序:< code> mongo-go ),这是我的问题,我要执行此功能

  db.rmTickets.find()。forEach(function(doc){  
 doc.created =新日期(doc.created)
 doc.updated =新日期(doc.updated)
 doc.deadline =新日期(doc.deadline)
 doc.dateEstimationDelivery =新日期(doc。  dateEstimationDelivery)
 doc.dateTransmitDemand =新日期(doc.dateTransmitDemand)
 doc.dateTransmitQuotation =新日期(doc.dateTransmitQuotation)
 doc.dateValidationQuotation =新日期(doc.dateValidationQuotation)
 doc.dateDeliveryCS =新日期(  doc.dateDeliveryCS)
 db.rmTickets.save(doc)
})
   
 
 

我在godoc上看到了 Database.RunCommand()< / code>存在,但我不确定如何使用它。 如果有人可以提供帮助:) 谢谢

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • donglaogu3788 2019-01-04 21:52
    已采纳

    RunCommand is to execute a mongo command. What you intend to do are to find all documents of a collection, make changes, and then replace them. You need Find(), cursor, and ReplaceOne(). Here is a similar code snippet.

    if cur, err = collection.Find(ctx, bson.M{"hometown": bson.M{"$exists": 1}}); err != nil {
        t.Fatal(err)
    }
    var doc bson.M
    for cur.Next(ctx) {
        cur.Decode(&doc)
        doc["updated"] = time.Now()
        if result, err = collection.ReplaceOne(ctx, bson.M{"_id": doc["_id"]}, doc); err != nil {
            t.Fatal(err)
        }
        if result.MatchedCount != 1 || result.ModifiedCount != 1 {
            t.Fatal("replace failed, expected 1 but got", result.MatchedCount)
        }
    }
    

    I have a full example TestReplaceLoop()

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题