dqrsceg6279196 2015-08-30 15:49
浏览 207

mgo find将单个值数组转换为字符串

This is part of my collection schema in mongodb:

{ "_id" : ObjectId("55e1eef5255da6d384754642"), "name" : [ "Web, Mobile & Software Dev", "Movil y desarrollo de software" ] } { "_id" : ObjectId("55e1f2d0255da6d38475464b"), "name" : [ "IT & Networking", "TI y Redes" ] } ...

Right now i can get the info like this:

 err := r.Coll.Find(bson.M{}).Select(bson.M{"name": bson.M{"$slice": []int{1, 1}}}).All(&result.Data)

but i want "name" to return a string instead of a single value array, so i dont have to index it inside my frontend if no need.

  • 写回答

1条回答

  • douhuan1908 2015-08-30 22:30
    关注

    very limited comments i need 2000 poits for editing my post and add more things it seems, this is not answer but maybe, so i have to loop it?, isnt better way?

    err := r.Coll.Find(bson.M{}).Select(bson.M{"name": bson.M{"$slice": []int{1, 1}}}).All(&result.Data)                                            
      if err != nil {                                                                                                                                 
          return result, err                                                                                                                          
      }                                                                                                                                               
    
      type skillnew struct {                                                                                                                          
          Id   bson.ObjectId `json:"id,omitempty" bson:"_id,omitempty"`                                                                               
          Name string        `bson:"name,omitempty" json:"name,omitempty"`                                                                            
      }                                                                                                                                               
    
      skillsallnew := make([]skillnew, len(result.Data))                                                                                              
      for i := range result.Data {                                                                                                                    
          skillsallnew[i] = skillnew{result.Data[i].Id, result.Data[i].Name[0]}                                                                       
      }    
    
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮