如何搜索记录然后将其删除 [英] How to search for a record and then delete it

查看:57
本文介绍了如何搜索记录然后将其删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过手机号搜索令牌记录.然后删除该记录?

How to search for the ttoken record by mobile no. and then delete that record?

User.rb:

field :ttokens, type: Hash, :default => {} # Stored as hash over numbers

通常,在rails控制台中,ttoken的值如下:

Normally the value of ttokens, in the rails console, are as follows:

ttokens: {"919839398393"=>{"atoken"=>"f704e803061e594150b09ad8akabfc6105ac85ab", "confirmed"=>true}, "91812798765"=>{"atoken"=>"255cb70926978b93eik67edb52fa23a163587b4b", "confirmed"=>true}}

我需要mongodb查询来通过手机号码搜索令牌记录,然后删除该记录.使用的数据库是MongoDB.任何指导或帮助将不胜感激.

I need a mongodb query to search for the ttoken record by mobile number and then delete that record. The DB used is MongoDB. Any guidance or help would be highly appreciated.

推荐答案

您需要对嵌入的元素使用MongoDB的点符号",这意味着键"必须是字符串类型的符号.还要应用 $exists 进行匹配存在密钥的位置,以及 .unset() 方法蒙古包:

You need to use MongoDB 'dot notation' for the embedded element, which means the "key" must be a string type of notation. Also apply $exists to match where the key in the has is present and the .unset() method from mongoid:

User.where('_id'=> userId, 'ttokens.919839398393'=> { '$exists' => true }).unset(
  'ttokens.919839398393'
)

这实际上是 $unset MongoDB运算符,它通过指定的路径从文档中删除键".

This is effectively the $unset operator of MongoDB, which removes "keys" from the document by the path specified.

这将从示例文档中匹配并删除第一个键,仅保留另一个.

From the sample document this would match and remove the first key, leaving only the other.

这篇关于如何搜索记录然后将其删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆