Để truy vấn MongoDB bằng toán tử $ ne, sau đây là cú pháp -
db.yourCollectionName.find({yourFieldName:{$ne:yourValue}}).pretty(); Hãy để chúng tôi tạo một bộ sưu tập với các tài liệu -
> db.notEqaulToDemo.insertOne({"StudentName":"Larry","StudentMathMarks":68});
{
"acknowledged" : true,
"insertedId" : ObjectId("5cbd3a6bde8cc557214c0ded")
}
> db.notEqaulToDemo.insertOne({"StudentName":"Chris","StudentMathMarks":88});
{
"acknowledged" : true,
"insertedId" : ObjectId("5cbd3a79de8cc557214c0dee")
}
> db.notEqaulToDemo.insertOne({"StudentName":"David","StudentMathMarks":45});
{
"acknowledged" : true,
"insertedId" : ObjectId("5cbd3a89de8cc557214c0def")
}
> db.notEqaulToDemo.insertOne({"StudentName":"Carol","StudentMathMarks":69});
{
"acknowledged" : true,
"insertedId" : ObjectId("5cbd3a97de8cc557214c0df0")
} Hiển thị tất cả các tài liệu từ một bộ sưu tập với sự trợ giúp của phương thức find () -
> db.notEqaulToDemo.find().pretty();
Điều này sẽ tạo ra kết quả sau -
{
"_id" : ObjectId("5cbd3a6bde8cc557214c0ded"),
"StudentName" : "Larry",
"StudentMathMarks" : 68
}
{
"_id" : ObjectId("5cbd3a79de8cc557214c0dee"),
"StudentName" : "Chris",
"StudentMathMarks" : 88
}
{
"_id" : ObjectId("5cbd3a89de8cc557214c0def"),
"StudentName" : "David",
"StudentMathMarks" : 45
}
{
"_id" : ObjectId("5cbd3a97de8cc557214c0df0"),
"StudentName" : "Carol",
"StudentMathMarks" : 69
} Sau đây là truy vấn cho toán tử MongoDB $ ne -
> db.notEqaulToDemo.find({StudentMathMarks:{$ne:88}}).pretty(); Điều này sẽ tạo ra kết quả sau -
{
"_id" : ObjectId("5cbd3a6bde8cc557214c0ded"),
"StudentName" : "Larry",
"StudentMathMarks" : 68
}
{
"_id" : ObjectId("5cbd3a89de8cc557214c0def"),
"StudentName" : "David",
"StudentMathMarks" : 45
}
{
"_id" : ObjectId("5cbd3a97de8cc557214c0df0"),
"StudentName" : "Carol",
"StudentMathMarks" : 69
}