簡體   English   中英

如何使用原子查詢更新貓鼬中另一個子文檔中的子文檔?

[英]How to update a subdocument inside another subdocument in mongoose using atomic query?

我的貓鼬模式如下所示:

var MousePos = Schema({
    x: { type: Number},
    y: { type: Number},
    clickCount: {type: Number, default:0}
});

var MouseTrackInfo = Schema({
    pos:{ type: [MousePos], default:[]},
    element: {type: String, index:true},
    clickCount:{ type: Number, default: 0 },
    timeSpent: { type: Number, default: 0 }
});

var MouseTrackSchema = Schema({
    pageUrl: { type: String},
    applicationId: { type: String, index: true },
    mouseTrackLog: { type: [MouseTrackInfo], default:[]},
    urlId: {type: String, index: true}
});

現在,我要執行的操作是在指定元素x和y值時更新pos子文檔中的clickCount。

我嘗試了多種方法,但似乎無濟於事。 此外,我也不能在查詢中使用兩個$運算符。 歡迎提出建議。 我想使此查詢盡可能原子。

我無法找出一種可靠的方法,因此我將架構更改為以下內容:

var MouseTrackInfo = Schema({
    element: {type: String, index:true},
    clickCount:{ type: Number, default: 0 },
    timeSpent: { type: Number, default: 0 },
    x: { type: Number, default:0},
    y: { type: Number, default: 0},
    identifier: {type: String}
});

var MouseTrackSchema = Schema({
    pageUrl: { type: String},
    applicationId: { type: String, index: true },
    mouseTrackLog: { type: [MouseTrackInfo], default:[]},
    urlId: {type: String, index: true}
});

MouseTrackSchema.methods.incrementClickCount = function(element,pageX,pageY,cb) {
    var parent = this;
    this.model('MouseTrackModel').update(
        {
            'applicationId':this.applicationId, 
            'urlId':this.urlId, 
            'mouseTrackLog.identifier': element+"X"+pageX+"Y"+pageY
        },
        {
            '$inc':{'mouseTrackLog.$.clickCount':1}
        },
        {
            'new': true
        },
        cb);
}

這使我可以執行原子查詢。 任何其他解決方案都歡迎。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM