- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我需要将类型字符串转换为 bson.ObjectId,这是我当前的代码:
type CampaignUpdateBody struct {
CampaignName *string `json:"campaign_name" bson:"campaign_name"`
FromName []string `json:"from_name" bson:"from_name"`
FromEmail *string `json:"from_email" bson:"from_email"`
ReplyEmail *string `json:"reply_email" bson:"reply_email"`
Subject []string `json:"subject" bson:"subject"`
BodyText *string `json:"body_text" bson:"body_text"`
BodyHTML *string `json:"body_html" bson:"body_html"`
SmtpList *string `json:"smtp_list_id" bson:"smtp_list"`
EmailList *string `json:"email_list_id" bson:"email_list"`
}
// LetterTemplateUpdate updates some fields of the letter template.
func (s *Service) CampaignUpdate(c *gin.Context) {
id := bson.ObjectIdHex(c.Param("id"))
if !id.Valid() {
c.JSON(http.StatusBadRequest, gin.H{"error": "id has wrong format"})
return
}
var body CampaignUpdateBody
if err := c.BindJSON(&body); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
token := c.MustGet(tokenKey).(*models.Token)
params := storage.CampaignUpdateParams{}
params.ID = id
//..........................
params.BodyText = body.BodyText
params.BodyHTML = body.BodyHTML
params.SmtpList = body.SmtpList
params.EmailList = body.EmailList
stor := c.MustGet(storageKey).(storage.Storage)
if err := stor.CampaignUpdate(token.UserID, params); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{})
}
这是我当前的错误:
..\httpservice\campaigns.go:134:19: cannot use body.SmtpList (type *string) as t
ype bson.ObjectId in assignment
..\httpservice\campaigns.go:135:19: cannot use body.EmailList (type *string) as
type bson.ObjectId in assignment
我需要将 body.SmtpList 类型转换为 bson.ObjectId 中的 *string,我该怎么做?
最佳答案
根据文档,bson.ObjectId
定义为
type ObjectId string
鉴于此,您应该能够使用
params.SmtpList = bson.ObjectId(*body.SmtpList)
这是一种类型转换,它之所以有效,是因为 ObjectId
的基础类型是 string
。
请注意,在执行此操作之前,您需要确保 body.SmtpList
不是 nil
,否则您的代码会出现 panic。
关于go - 将类型 *string 转换为类型 bson.ObjectId,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54384053/
假设我在文件开头有这个定义: const ObjectId = mongoose.Types.ObjectId; 您更喜欢哪一个,为什么? // 1 new ObjectId; // 2 new O
我通过以下两种不同的方法生成了一个 ObjectId: user@ubuntu:~$ python Python 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24)
我编写了一个 Python 代码,用于从名为 Tweets 的 Mongo 集合中获取推文。我不想只获取对象文本并添加一个名为 Sentiment 的附加对象。 当我循环推文并将 json 对象解析为
我是 Node.js 和 mongodb 的新手。 在我的 Node.js 应用程序中,我使用的是 mongodb,对于 mongodb 操作,我使用的是 mongoose。 在 package.js
使用 parse.com 和 JavaScript SDK。 这就是我想要实现的目标。 页面上将用户( friend )列表返回给用户,然后他们可以单击其中一个用户,页面将更新以列出所有该用户的项目(
我有一个像这样的 objectId:["56153e4c2040efa61b4e267f","56033932efefe0d8657bbd9e"]为了在我的模型中保存该信息,我使用: items: [
我有一个标签模式: const TagSchema = new Schema({ label: {type: String, unique: true, minlength: 2}, upda
在online API他们指的是 Mongo::ObjectID。 我有 require 'mongo' 但 ruby 仍然找不到它。例如,我需要通过它的 Id 找到一个对象,我正在做: mong
作为管道的最后阶段,我有一个像这样的 $project : { ... anId : new ObjectId() } 但是 mongo 为每个文档生成相同的 Id。我希望它为每个投影文档生成
我需要在这样的数组中找到 mongoose objectID 的索引: [ { _id: 58676b0a27b3782b92066ab6, score: 0 }, { _id: 58676aca
我们可以从客户端生成 ObjectId 并在插入时使用它。我确实希望它在插入过程之外进行处理。我需要将其配置为我的默认 _id 生成过程,以便当我调用 insert 时,insert 方法应该创建自定
这是代码。跟随者和追随者无法工作。换句话说,我无法将像 {"user1", objectId1} 这样的对象推送到这两个数组。是否允许有一个字段同时包含对象 ID 和字符串名称?感谢您的帮助。 The
为什么 Meteor.js 使用它自己的 IDsp 算法> 为什么不使用 MongoDB ObjectId()? 最佳答案 如果你选择使用 Meteor,它对对象 ID 使用相同的方法: Meteor
Mongoose 菜鸟问题:我有两个 Mongoose ObjectId 对象列表,我将它们合并为一个。有些 ObjectId 是重复的,我不想将它们保存到我的数据库中。是否有 Mongoose 工具
以下代码从供应商集合中的员工数组中提取员工 await new VendorManager() .update( { emplo
我有一个简单的模型对象: class UserRating include MongoMapper::EmbeddedDocument key :idea_id, ObjectId key
谁能告诉我使用 #[objectId] 或 [id=objectId] 引用元素有什么区别? 最佳答案 第一个非常快,因为 jQuery 内部使用 getElementById当它识别出模式时(使用正
我的架构是: var schema = new Schema({ players: { type: [Schema.Types.ObjectId]}); 我正在使用 promi
这似乎是这样定义我的架构: var PossessionSchema = new mongoose.Schema({ thing: {type: mongoose.Schema.Types.Obj
我想要解决的是:使用建议的方法(mapReduce)保留 $in 中的 Id 数组的顺序: Does MongoDB's $in clause guarantee order 我已经完成了我的作业,发
我是一名优秀的程序员,十分优秀!