欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

MongoDB指定类型查询数据

发布时间:2025/6/15 编程问答 33 豆豆
生活随笔 收集整理的这篇文章主要介绍了 MongoDB指定类型查询数据 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

1.查看集合

show collections

2.向集合中添加数据

db.runoob.insertMany([

{

'title':'data1',

'url':'https://blog.51cto.com/suyanzhu',

'description':'this is data1',

'view':5000

},

{

'title':'data2',

'url':'https://blog.51cto.com/suyanzhu',

'description':'this is data2',

'view':3650

},

{

'title':'data3',

'url':'https://blog.51cto.com/suyanzhu',

'description':'this is data3',

'view':9527

}

])

3.向集合添加数据

db.runoob.insert({

        title: 222, 

        description: 'mongodb',

        by: 'suyanzhu',

        url: 'https://blog.51cto.com/suyanzhu',

        tags: ['php','mongodb'],

        likes: 200

})

4.查看所有数据

db.runoob.find().pretty()

5.根据类型查询数据

// 查询title类型为字符串的数据

db.runoob.find({'title':{$type:2}}).pretty()

// 或

db.runoob.find({'title':{$type:'string'}}).pretty()


//查询title类型为数值的数据

db.runoob.find({'title':{$type:1}}).pretty()

// 或

db.runoob.find({'title':{$type:'double'}}).pretty()

转载于:https://blog.51cto.com/suyanzhu/2307917

总结

以上是生活随笔为你收集整理的MongoDB指定类型查询数据的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。