search

Home  >  Q&A  >  body text

mongodb - Mongo java驱动中,有没有封装好的函数,可以直接取到文档中的某个具体的值,而不是一个文档?或者有没有方法实现??

{"_id":"37389873",
"Date":{
"name":{"count":"7",
"user": "liyang",
}

     }

}

比如数据库中存的这个文档,我想取count中的值7,只取数字7,不要文档,改怎么实现呢??

滿天的星座滿天的星座2764 days ago814

reply all(5)I'll reply

  • 过去多啦不再A梦

    过去多啦不再A梦2017-04-24 16:02:54

    Finally, I used Jackson to convert the json string into a map, and then used map to slice it layer by layer, and finally returned a numerical value

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-24 16:02:54

    db.xxx.findOne({"_id":"37389873"}, {"Date.name.count": 1}).Date.name.count

    Operation effect

    ~/tmp
    ▶ cat mmm.js
    #!/usr/bin/env mongo
    
    var db = connect('test');
    db.xxx.drop();
    db.xxx.insert({
      "_id": "37389873",
      "Date":{
             "name":{"count":"7",
                     "user": "liyang",
                     }
    
             }
    
     });
    
    print( db.xxx.findOne({"_id":"37389873"}, {"Date.name.count": 1}).Date.name.count );
    
    
    ~/tmp
    ▶ ./mmm.js
    MongoDB shell version: 2.6.3
    connecting to: test
    connecting to: test
    7
    

    reply
    0
  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-04-24 16:02:54

    http://jongo.org/
    Maybe this is what you are looking for

    reply
    0
  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-04-24 16:02:54

    spring’s mongodb plug-in can satisfy

    BasicQuery query = new BasicQuery(query,fields);
    

    query is the query condition
    fields is the field to be queried

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-24 16:02:54

    collections.find(docQuery).projection(keys)

    reply
    0
  • Cancelreply