如果密钥存在,则附加或在mongodb中创建新密钥

时间:2016-01-05 05:56:58

标签: mongodb

我有一张带有字段的巨大Excel表:

  • CUSTOMER_ID
  • 活性
  • 时间戳

我必须阅读excel表并将值插入集合中。如果存在具有给定 customer_id 键的文档,则应将活动附加到活动键,否则应创建新文档。
在插入之前,我是否需要检查每个 customer_id 是否存在?有效的方法是什么?

1 个答案:

答案 0 :(得分:1)

你可以使用$ push组合使用upsert来完成此操作。请参阅以下示例


db.tempc.find()
{ "_id" : 1, "activities" : [ "Activity1.1" ] }
 

然后观察结果


record = {customerId: 1, activity: 'Activity1.2'}
db.tempc.update({_id: record.customerId}, {$push: {activities: record.activity}}, {upsert: true})

然后尝试处理具有相同customerId但活动不同的第二条记录


db.tempc.find()
{ "_id" : 1, "activities" : [ "Activity1.1", "Activity1.2" ] }

现在观察此活动是否已添加到现有客户

<body>
    <div class="ng-scope" data-ng-app="myApp">
        <div class="ng-scope ng-binding" data-ng-controller="myController">

            <div style="" class="k-grid k-widget" data-role="grid" kendo-grid="kGrid" options="mainGridOptions"><div class="k-header k-grid-toolbar ng-scope"><a class="k-button k-button-icontext k-grid-add" href="#"><span class="k-icon k-add"></span>Add new record</a></div><div style="padding-right: 17px;" class="k-grid-header"><div class="k-grid-header-wrap k-auto-scrollable"><table role="grid"><colgroup><col><col style="width:100px"><col style="width:100px"><col style="width:180px"></colgroup><thead role="rowgroup"><tr role="row"><th class="k-header ng-scope" role="columnheader" data-field="aname" rowspan="1" data-title="Article" data-index="0" id="0a08329f-d7f3-48d5-933b-e72b9d3c9a7f">Article</th><th class="k-header ng-scope" role="columnheader" data-field="price" rowspan="1" data-title="Price" data-index="1" id="273ccd7b-5585-4e45-a815-438d9ee68fbf">Price</th><th class="k-header ng-scope" role="columnheader" data-field="qty" rowspan="1" data-title="Quantity" data-index="2" id="afe572ff-58f7-4923-a03c-755e7c841e31">Quantity</th><th class="k-header ng-scope" id="ce99e1ab-46a8-4b4c-8f92-6277477ba4d9" rowspan="1" data-index="3">&nbsp;</th></tr></thead></table></div></div><div class="k-grid-content k-auto-scrollable"><table role="grid"><colgroup><col><col style="width:100px"><col style="width:100px"><col style="width:180px"></colgroup><tbody role="rowgroup"></tbody></table><div style="width: 1245px;" class="k-grid-content-expander"></div></div></div>

            <br>
            <br>
            Lines number  : <span id="spnCount"></span>
            <!--{{ kGrid.dataSource.data().length }}-->

        </div>
    </div>
    <script>
        var ultiApp = angular.module('myApp', ['kendo.directives']);

        var docLines = [];
        var counter = docLines.length;

        ultiApp.controller('myController', function ($scope) {

            $scope.mainGridOptions = {
                dataSource: {
                    transport: {
                        read: function (o) { o.success(docLines); },
                        create: function (o) {
                            var item = o.data;
                            item.id = counter++;
                            o.success(item);
                            document.getElementById("spnCount").innerHTML = counter;
                        },
                        update: function (o) { o.success(); },
                        destroy: function (o) {
                            o.success();
                            counter--;
                            document.getElementById("spnCount").innerHTML = counter;
                        }
                    },
                    schema: {
                        model: {
                            id: "id",
                            fields: {
                                aname: { validation: { required: true } },
                                price: { type: "number", validation: { min: 0, required: true } },
                                qty: { type: "number", validation: { min: 0, required: true } }
                            }
                        }
                    }
                },

                pageable: false,
                toolbar: ["create"],
                editable: "popup",
                columns: [
                  { field: "aname", title: "Article" },
                  { field: "price", title: "Price", format: "{0:c}", width: "100px" },
                  { field: "qty", title: "Quantity", width: "100px" },
                  { command: ["edit", "destroy"], title: "&nbsp;", width: "180px" }
                ]
            }

        });
    </script>
</body>