如何在 mongoid 中对选定属性进行投影?

发布于 2024-12-11 06:12:23 字数 1903 浏览 1 评论 0原文

我有带有许多嵌入在用户模型中的标记的坐标模型。如何提取没有 _ids 的属性,以便在输出中仅显示每个 lng 和 lat?

结构:

{ "_id" : ObjectId( "4e9f418f1e7bf20fbc000009" ),
  "name" : "test",
  "coordinates" : [ 
    { "lng" : 16.86783310009764,
      "lat" : 52.38353842845282,
      "_id" : ObjectId( "4e9f418f1e7bf20fbc00000a" ) }, 
    { "lng" : 16.85787674023436,
      "lat" : 52.40972501601293,
      "_id" : ObjectId( "4e9f418f1e7bf20fbc00000b" ) }, 
    { "lng" : 16.92276474072264,
      "lat" : 52.40071858320756,
      "_id" : ObjectId( "4e9f418f1e7bf20fbc00000c" ) }, 
    { "lng" : 16.90182205273436,
      "lat" : 52.38270020105396,
      "_id" : ObjectId( "4e9f418f1e7bf20fbc00000d" ) }, 
    { "lng" : 16.96705337597655,
      "lat" : 52.410661698108,
      "_id" : ObjectId( "4e9f418f1e7bf20fbc00000e" ) }, 
    { "lng" : 16.89495559765624,
      "lat" : 52.42773236584494,
      "_id" : ObjectId( "4e9f418f1e7bf20fbc00000f" ) } ] }

例如

= debug @user.coordinates.to_json

给出:

--- ! '[{"_id":"4e9f418f1e7bf20fbc00000a","lat":52.383538428452816,"lng":16.86783310009764},{"_id":"4e9f418f1e7bf20fbc00000b","lat":52.40972501601293,"lng":16.85787674023436},{"_id":"4e9f418f1e7bf20fbc00000c","lat":52.40071858320756,"lng":16.92276474072264},{"_id":"4e9f418f1e7bf20fbc00000d","lat":52.382700201053964,"lng":16.90182205273436},{"_id":"4e9f418f1e7bf20fbc00000e","lat":52.410661698108,"lng":16.967053375976548},{"_id":"4e9f418f1e7bf20fbc00000f","lat":52.42773236584494,"lng":16.894955597656235}]'

预期:

--- ! '[{"lat":52.383538428452816,"lng":16.86783310009764},{"lat":52.40972501601293,"lng":16.85787674023436},{"lat":52.40071858320756,"lng":16.92276474072264},{"lat":52.382700201053964,"lng":16.90182205273436},{"lat":52.410661698108,"lng":16.967053375976548},{"lat":52.42773236584494,"lng":16.894955597656235}]'

I have Coordinate model with many markers embedded_in User model. How to extract attributes without _ids so that on output display only each lng and lat?

structure:

{ "_id" : ObjectId( "4e9f418f1e7bf20fbc000009" ),
  "name" : "test",
  "coordinates" : [ 
    { "lng" : 16.86783310009764,
      "lat" : 52.38353842845282,
      "_id" : ObjectId( "4e9f418f1e7bf20fbc00000a" ) }, 
    { "lng" : 16.85787674023436,
      "lat" : 52.40972501601293,
      "_id" : ObjectId( "4e9f418f1e7bf20fbc00000b" ) }, 
    { "lng" : 16.92276474072264,
      "lat" : 52.40071858320756,
      "_id" : ObjectId( "4e9f418f1e7bf20fbc00000c" ) }, 
    { "lng" : 16.90182205273436,
      "lat" : 52.38270020105396,
      "_id" : ObjectId( "4e9f418f1e7bf20fbc00000d" ) }, 
    { "lng" : 16.96705337597655,
      "lat" : 52.410661698108,
      "_id" : ObjectId( "4e9f418f1e7bf20fbc00000e" ) }, 
    { "lng" : 16.89495559765624,
      "lat" : 52.42773236584494,
      "_id" : ObjectId( "4e9f418f1e7bf20fbc00000f" ) } ] }

e.g.

= debug @user.coordinates.to_json

gives:

--- ! '[{"_id":"4e9f418f1e7bf20fbc00000a","lat":52.383538428452816,"lng":16.86783310009764},{"_id":"4e9f418f1e7bf20fbc00000b","lat":52.40972501601293,"lng":16.85787674023436},{"_id":"4e9f418f1e7bf20fbc00000c","lat":52.40071858320756,"lng":16.92276474072264},{"_id":"4e9f418f1e7bf20fbc00000d","lat":52.382700201053964,"lng":16.90182205273436},{"_id":"4e9f418f1e7bf20fbc00000e","lat":52.410661698108,"lng":16.967053375976548},{"_id":"4e9f418f1e7bf20fbc00000f","lat":52.42773236584494,"lng":16.894955597656235}]'

expected:

--- ! '[{"lat":52.383538428452816,"lng":16.86783310009764},{"lat":52.40972501601293,"lng":16.85787674023436},{"lat":52.40071858320756,"lng":16.92276474072264},{"lat":52.382700201053964,"lng":16.90182205273436},{"lat":52.410661698108,"lng":16.967053375976548},{"lat":52.42773236584494,"lng":16.894955597656235}]'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

地狱即天堂 2024-12-18 06:12:23

尝试使用例外

 @user.coordinates.to_json(:except => '_id')

Try using except

 @user.coordinates.to_json(:except => '_id')
捶死心动 2024-12-18 06:12:23

接受的答案返回您想要的数据。我建议对 mongoid 查询使用 without ,这样你实际上就不会从数据库中获取数据,这是理想的。

@user.coordinates.without('_id').to_json(:except => '_id')

the accepted answer returns data you wished. I would suggest though to use without against mongoid query so you actually do not fetch the data from DB, which is ideal.

@user.coordinates.without('_id').to_json(:except => '_id')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文