千家信息网

MongoDB中类似模糊查询操作

发表于:2025-02-05 作者:千家信息网编辑
千家信息网最后更新 2025年02月05日,1.查看集合show collections2.向集合中添加数据db.runoob.insertMany([{'title':'data1','url':'https://blog.51cto.com
千家信息网最后更新 2025年02月05日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.find().pretty()

4.查询包含指定字符串的数据

// title中包含a1字符串的数据

db.runoob.find({'title':/a1/}).pretty()

5.查询以指定字符串开头的数据

// title以d开头的数据

db.runoob.find({'title':/^d/}).pretty()

6.查询以指定字符串结尾的数据

// title以a3结尾的数据

db.runoob.find({'title':/a3$/}).pretty()

0