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]}                                                                       
      }    
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?