我想用SQL查询一些东西,比如查询:
SELECT * FROM users WHERE name LIKE '%m%'
如何在MongoDB中实现这一点?我在文档中找不到这样的操作员。
我想用SQL查询一些东西,比如查询:
SELECT * FROM users WHERE name LIKE '%m%'
如何在MongoDB中实现这一点?我在文档中找不到这样的操作员。
当前回答
如果您使用的是PHP,可以使用MongoDB数据对象包装器,如下所示:
$model = new MongoDB_DataObject();
$model->query("select * from users where name like '%m%'");
while($model->fetch()) {
var_dump($model);
}
Or:
$model = new MongoDB_DataObject('users);
$model->whereAdd("name like '%m%'");
$model->find();
while($model->fetch()) {
var_dump($model);
}
其他回答
FullName类似于“last”,两个日期之间的状态==“结束”:
db.orders.find({
createdAt:{$gt:ISODate("2017-04-25T10:08:16.111Z"),
$lt:ISODate("2017-05-05T10:08:16.111Z")},
status:"Pending",
fullName:/last/}).pretty();
status==“待定”,orderId LIKE“PHA871674”:
db.orders.find({
status:"Pending",
orderId:/PHA876174/
}).pretty();
如果使用Node.js,它表示您可以编写以下内容:
db.collection.find( { field: /acme.*corp/i } );
// Or
db.collection.find( { field: { $regex: 'acme.*corp', $options: 'i' } } );
此外,您还可以这样写:
db.collection.find( { field: new RegExp('acme.*corp', 'i') } );
对于Node.js中的Mongoose:
db.users.find({'name': {'$regex': '.*sometext.*'}})
您可以使用MongoDB 2.6的新功能:
db.foo.insert({desc: "This is a string with text"});
db.foo.insert({desc:"This is a another string with Text"});
db.foo.ensureIndex({"desc":"text"});
db.foo.find({
$text:{
$search:"text"
}
});
在Go和mgo驱动程序中:
Collection.Find(bson.M{"name": bson.RegEx{"m", ""}}).All(&result)
其中结果是所查找类型的结构实例。