Hướng dẫn how do i compare two objects in mongodb? - làm cách nào để so sánh hai đối tượng trong mongodb?

Vì vậy, ý tưởng là

  1. Sắp xếp mảng customerSchemes bằng startDate.
  2. Chọn mục đầu tiên từ danh sách được sắp xếp.
  3. Chỉ bao gồm nó nếu customerSchemes.nameA.

Hãy thử truy vấn này:

db.collection.aggregate([
    { $unwind: "$customerSchemes" },
    {
        $sort: { "customerSchemes.startDate": 1 }
    },
    {
        $group: {
            _id: "$_id",
            customerSchemes: { $push: "$customerSchemes" }
        }
    },
    {
        $match: {
            $expr: {
                $eq: [{ $first: "$customerSchemes.name" }, "A"]
            }
        }
    }
]);

Output:

/* 1 createdAt:3/12/2021, 6:40:42 PM*/
{
    "_id" : ObjectId("604b685232a8d433d8ede6c4"),
    "customerSchemes" : [
        {
            "name" : "A",
            "startDate" : ISODate("2021-03-01T00:00:00.000+05:30")
        },
        {
            "name" : "B",
            "startDate" : ISODate("2021-03-02T00:00:00.000+05:30")
        }
    ]
},

/* 2 createdAt:3/12/2021, 6:40:42 PM*/
{
    "_id" : ObjectId("604b685232a8d433d8ede6c6"),
    "customerSchemes" : [
        {
            "name" : "A",
            "startDate" : ISODate("2021-03-01T00:00:00.000+05:30")
        },
        {
            "name" : "B",
            "startDate" : ISODate("2021-03-05T00:00:00.000+05:30")
        }
    ]
}

Dữ liệu kiểm tra:

/* 1 createdAt:3/12/2021, 6:40:42 PM*/
{
    "_id" : ObjectId("604b685232a8d433d8ede6c4"),
    "customerSchemes" : [
        {
            "name" : "A",
            "startDate" : ISODate("2021-03-01T00:00:00.000+05:30")
        },
        {
            "name" : "B",
            "startDate" : ISODate("2021-03-02T00:00:00.000+05:30")
        }
    ]
},

/* 2 createdAt:3/12/2021, 6:40:42 PM*/
{
    "_id" : ObjectId("604b685232a8d433d8ede6c5"),
    "customerSchemes" : [
        {
            "name" : "A",
            "startDate" : ISODate("2021-03-03T00:00:00.000+05:30")
        },
        {
            "name" : "B",
            "startDate" : ISODate("2021-03-02T00:00:00.000+05:30")
        }
    ]
},

/* 3 createdAt:3/12/2021, 6:40:42 PM*/
{
    "_id" : ObjectId("604b685232a8d433d8ede6c6"),
    "customerSchemes" : [
        {
            "name" : "B",
            "startDate" : ISODate("2021-03-05T00:00:00.000+05:30")
        },
        {
            "name" : "A",
            "startDate" : ISODate("2021-03-01T00:00:00.000+05:30")
        }
    ]
}

So sánh hai tài liệu đối tượng trong MongoDB 4.4 với đường ống tổng hợp chức năng $

Tệp này chứa văn bản unicode hai chiều có thể được giải thích hoặc biên dịch khác với những gì xuất hiện dưới đây. Để xem xét, hãy mở tệp trong một trình soạn thảo cho thấy các ký tự Unicode ẩn. Tìm hiểu thêm về các ký tự unicode hai chiều

/*
Đầu ra:
{
"_id": ObjectID ("5EEC4FD0AE4BF9D2E209E2D"),
"Diff": {
"Havemore": {
"FOO2": NULL
},
"Các loại": {
"DifferType": numberLong ("55"),
"Objectiddiff": ObjectID ("5EEC5020AE4BF9D2E209E2E"),
"StringDiff": "khác nhau",
"longdiff": numberLong ("99"),
"Decimaldiff": NumberDecimal ("11"),
"Datediff": Isodate ("2020-06-19T05: 44: 39.542z")
},
"Các loại": {
"DifferType": numberLong ("55"),
"Objectiddiff": ObjectID ("5EEC5020AE4BF9D2E209E2E"),
"StringDiff": "khác nhau",
"longdiff": numberLong ("99"),
"Decimaldiff": NumberDecimal ("11"),
"Datediff": Isodate ("2020-06-19T05: 44: 39.542z")
"Objectiddiff": ObjectID ("5EEC5020AE4BF9D2E209E2E"),
"StringDiff": "khác nhau",
"longdiff": numberLong ("99"),
"Decimaldiff": NumberDecimal ("11"),
"Datediff": Isodate ("2020-06-19T05: 44: 39.542z")
"ArrayDiff": [
"Objectiddiff": ObjectID ("5EEC5020AE4BF9D2E209E2E"),
"StringDiff": "khác nhau",
"ArrayDiff": [
"x"
],
db.a.aggregate([.a.aggregate([
{"$project":{"$project":{
"diff":{:{
"$function":{:{
"ArrayDifflength": [: function(objectA, objectB) {
vardiff=function(obj1,obj2){ diff = function (obj1, obj2) {
lettype1=Object.prototype.toString.call(obj1); type1 = Object.prototype.toString.call(obj1);
lettype2=Object.prototype.toString.call(obj2); type2 = Object.prototype.toString.call(obj2);
chưa xác định, (type2 === '[object Undefined]') {
returnobj1; obj1;
"x"
if(type1!==type2){ (type1 !== type2) {
returnobj1; obj1;
"x"
], (type1 === '[object ObjectId]' || type1 === '[object NumberDecimal]') {
if(obj1.toString()!==obj2.toString()){ (obj1.toString() !== obj2.toString()) {
returnobj2; obj2;
"x"
"x"
], (type1 === "[object Date]" || type1 === "[object NumberLong]"){
if(obj1.valueOf()!==obj2.valueOf()){ (obj1.valueOf() !== obj2.valueOf()) {
returnobj2; obj2;
"x"
"x"
], (type1 === "[object String]") {
if(obj1!==obj2){ (obj1 !== obj2) {
returnobj2; obj2;
"x"
"x"
], (type1 === '[object BSON]') {
letresult={} result = {}
for(letkeyinobj1){ (let key in obj1) {
if(obj2.hasOwnProperty(key)){ (obj2.hasOwnProperty(key)) {
lettmp=diff(obj1[key],obj2[key]); tmp = diff(obj1[key], obj2[key]);
if(tmp!==undefined){ (tmp !== undefined){
result[key]=tmp;[key] = tmp;
"x"
}else{ else {
result[key]=null;[key] = null;
"x"
"x"
if(Object.keys(result).length>0){ (Object.keys(result).length > 0){
returnresult; result;
}else{ else {
returnundefined; undefined;
"x"
"x"
], (type1 === '[object Array]') {
letresult=[]; result = [];
for(leti=0;i (let i = 0; i < obj1.length; i++) {
lettmp=diff(obj1[i],obj2[i]); tmp = diff(obj1[i], obj2[i]);
if(tmp!==undefined){ (tmp !== undefined){
result[i]=tmp;[i] = tmp;
"x"
"x"
if(result.length>0){(result.length > 0){
returnresult; result;
}else{ else {
returnundefined; undefined;
"x"
"x"
returnundefined; undefined;
],;
returndiff(objectA,objectB); diff(objectA, objectB);
"ArrayDifflength": [,
chưa xác định,:[ {"$arrayElemAt": ["$docs", 0]}, {"$arrayElemAt":["$docs", 1]}],
"lang":"js"}:"js"}
"x"
],}
]).pretty()).pretty()

"ArrayDifflength": [

{
chưa xác định,: ObjectId("5eec4fd0aee4bf9d2e209e2d"),
"B",: [
{
"c": {
"ArrayObjects": [: "x1",
{: "x2"
"ArrayDifflength": [,
chưa xác định,: {
"B",: "x1"
"ArrayDifflength": [,
chưa xác định,: {
"B",: NumberLong("55"),
"c": ObjectId("5eec4f0aaee4bf9d2e209e2b"),
"ArrayObjects": [: ObjectId("5eec4f0aaee4bf9d2e209e2c"),
{: "equal",
"Xoo": null: "equal",
}: NumberLong("11"),
"Missingfield": null: NumberLong("11"),
}: NumberDecimal("99"),
*/: NumberDecimal("99"),
"Body": Function (Objecta, ObjectB) {: ISODate("2020-06-19T05:36:21.995Z"),
if (type2 === '[đối tượng không xác định]') {: ISODate("2020-06-19T05:36:22.995Z"),
"ArrayDifflength": [,
chưa xác định,: ["a", "b"],
"B",: ["a", "b", "c"],
"c": [
"ArrayObjects": ["foo":{"bar": "baz"}},
{"xoo":{"xar": "xaz"}}
"Xoo": null,
}: "x"
"ArrayDifflength": [,
{
"c": {
"ArrayObjects": [: "x1",
"ArrayDifflength": [,
chưa xác định,: {
"B",: "x1",
"c": "x2"
"ArrayDifflength": [,
chưa xác định,: {
"B",: "55",
"c": ObjectId("5eec4f0aaee4bf9d2e209e2b"),
"ArrayObjects": [: ObjectId("5eec5020aee4bf9d2e209e2e"),
{: "equal",
"Xoo": null: "different",
}: NumberLong("11"),
"Missingfield": null: NumberLong("99"),
}: NumberDecimal("99"),
*/: NumberDecimal("11"),
"Body": Function (Objecta, ObjectB) {: ISODate("2020-06-19T05:36:21.995Z"),
if (type2 === '[đối tượng không xác định]') {: ISODate("2020-06-19T05:44:39.542Z"),
"ArrayDifflength": [,
chưa xác định,: ["a", "b"],
"B",: ["a"],
"c": [
"ArrayObjects": ["foo":{"bar": "baz"}},
{"yoo":{"yar": "yaz"}}
"Xoo": null,
"ArrayDifflength": [,
chưa xác định,
"x"

Trận đấu được sử dụng cho MongoDB là gì?

Giai đoạn tổng hợp này hoạt động như một phát hiện. Nó sẽ lọc các tài liệu phù hợp với truy vấn được cung cấp. Sử dụng $ khớp sớm trong đường ống có thể cải thiện hiệu suất vì nó giới hạn số lượng tài liệu mà các giai đoạn tiếp theo phải xử lý.filter documents that match the query provided. Using $match early in the pipeline can improve performance since it limits the number of documents the next stages must process.

Thứ tự chính xác trong đó MongoDB so sánh các loại là gì?

MongoDB sắp xếp bindata theo thứ tự sau: Đầu tiên, độ dài hoặc kích thước của dữ liệu. Sau đó, bởi BSON ONE-BYTE SUBTYPE.FINAL, BY THEO DỮ LIỆU, thực hiện so sánh byte-byte.First, the length or size of the data. Then, by the BSON one-byte subtype. Finally, by the data, performing a byte-by-byte comparison.

Bạn có thể sử dụng tìm và tổng hợp trong MongoDB không?

Để tạo một đường ống tổng hợp, bạn có thể sử dụng có thể sử dụng phương thức tổng hợp của MongoDB ().Phương thức này sử dụng một cú pháp khá giống với phương thức Find () được sử dụng để truy vấn dữ liệu trong một bộ sưu tập, nhưng tổng hợp () chấp nhận một hoặc nhiều tên giai đoạn làm đối số.you can use can use MongoDB's aggregate() method. This method uses a syntax that is fairly similar to the find() method used to query data in a collection, but aggregate() accepts one or more stage names as arguments.

Sự khác biệt giữa việc sử dụng tổng hợp () và find () trong MongoDB là gì?

Với tổng hợp + $ khớp, bạn có được một BSON nguyên khối lớn chứa tất cả các tài liệu phù hợp. Với tìm, bạn có một con trỏ cho tất cả các tài liệu phù hợp. Sau đó bạn có thể nhận từng tài liệu một. With find, you get a cursor to all matching documents. Then you can get each document one by one.