Mongoose find one and push to array of documents

后端 未结 1 1038
别跟我提以往
别跟我提以往 2021-01-20 17:42

I\'m new to MongoDB and Mongoose and I\'m trying to use it to save stock ticks for daytrading analysis. So I imagined this Schema:

symbolSchema = Schema({
           


        
相关标签:
1条回答
  • 2021-01-20 18:20

    Basically put an $addToSet operator cannot work for you because your data is not a true "set" by definition being a collection of "completely distinct" objects.

    The other piece of logical sense here is that you would be working on the data as it arrives, either as a sinlge object or a feed. I'll presume its a feed of many items in some form and that you can use some sort of stream processor to arrive at this structure per document received:

    {
        "date": new Date("2015-03-09 13:23:00.000Z"),
        "symbol": "AAPL",
        "open": 127.14
        "high": 127.17,
        "low": 127.12 
        "close": 127.15,
        "volume": 19734
    }
    

    Converting to a standard decimal format as well as a UTC date since any locale settings really should be the domain of your application once data is retrieved from the datastore of course.

    I would also at least flatten out your "intraDayQuoteSchema" a little by removing the reference to the other collection and just putting the data in there. You would still need a lookup on insertion, but the overhead of the additional populate on read would seem to be more costly than the storage overhead:

    intradayQuotesSchema = Schema({
        symbol:{
            name: String,
            code: String
        },
        day:Date,
        quotes:[quotesSchema]
    });
    

    It depends on you usage patterns, but it's likely to be more effective that way.

    The rest really comes down to what is acceptable to

    stream.on(function(data) {
    
        var symbol = data.symbol,
            myDay = new Date( 
                data.date.valueOf() - 
                    ( data.date.valueOf() % 1000 * 60 * 60 * 24 ));
        delete data.symbol;
    
        symbol.findOne({ "code": symbol },function(err,stock) {
    
            intraDayQuote.findOneAndUpdate(
                { "symbol.code": symbol , "day": myDay },
                { "$setOnInsert": { 
                   "symbol.name": stock.name
                   "quotes": [data] 
                }},
                { "upsert": true }
                function(err,doc) {
                    intraDayQuote.findOneAndUpdate(
                        {
                            "symbol.code": symbol,
                            "day": myDay,
                            "quotes.date": data.date
                        },
                        { "$set": { "quotes.$": data } },
                        function(err,doc) {
                            intraDayQuote.findOneAndUpdate(
                                {
                                    "symbol.code": symbol,
                                    "day": myDay,
                                    "quotes.date": { "$ne": data.date }
                                },
                                { "$push": { "quotes": data } },
                                function(err,doc) {
    
                                }
                           );    
                        }
                    );
                }
            );    
        });
    });
    

    If you don't actually need the modified document in the response then you would get some benefit by implementing the Bulk Operations API here and sending all updates in this package within a single database request:

    stream.on("data",function(data) {
    
        var symbol = data.symbol,
            myDay = new Date( 
                data.date.valueOf() - 
                    ( data.date.valueOf() % 1000 * 60 * 60 * 24 ));
        delete data.symbol;
    
         symbol.findOne({ "code": symbol },function(err,stock) {
             var bulk = intraDayQuote.collection.initializeOrderedBulkOp();
             bulk.find({ "symbol.code": symbol , "day": myDay })
                 .upsert().updateOne({
                     "$setOnInsert": { 
                         "symbol.name": stock.name
                         "quotes": [data] 
                     }
                 });
    
             bulk.find({
                 "symbol.code": symbol,
                 "day": myDay,
                 "quotes.date": data.date
             }).updateOne({
                 "$set": { "quotes.$": data }
             });
    
             bulk.find({
                 "symbol.code": symbol,
                 "day": myDay,
                 "quotes.date": { "$ne": data.date }
             }).updateOne({
                 "$push": { "quotes": data }
             });
    
             bulk.execute(function(err,result) {
                 // maybe do something with the response
             });            
         });
    });
    

    The point is that only one of the statements there will actually modify data, and since this is all sent in the same request there is less back and forth between the application and server.

    The alternate case is that it might just be more simple in this case to have the actual data referenced in another collection. This then just becomes a simple matter of processing upserts:

    intradayQuotesSchema = Schema({
        symbol:{
            name: String,
            code: String
        },
        day:Date,
        quotes:[{ type: Schema.Types.ObjectId, ref: "quote" }]
    });
    
    
    // and in the steam processor
    
    stream.on("data",function(data) {
    
        var symbol = data.symbol,
            myDay = new Date( 
                data.date.valueOf() - 
                    ( data.date.valueOf() % 1000 * 60 * 60 * 24 ));
        delete data.symbol;
    
        symbol.findOne({ "code": symbol },function(err,stock) {
             quote.update(
                { "date": data.date },
                { "$setOnInsert": data },
                { "upsert": true },
                function(err,num,raw) {
                    if ( !raw.updatedExisting ) {
                        intraDayQuote.update(
                            { "symbol.code": symbol , "day": myDay },
                            { 
                                "$setOnInsert": {
                                    "symbol.name": stock.name
                                },
                                "$addToSet": { "quotes": data }
                            },
                            { "upsert": true },
                            function(err,num,raw) {
    
                            }
                        );
                    }
                }
            );
        });
    });
    

    It really comes down to how important to you is it to have the data for quotes nested within the "day" document. The main distinction is if you want to query those documents based on the data some of those "quote" fields or otherwise live with the overhead of using .populate() to pull in the "quotes" from the other collection.

    Of course if referenced and the quote data is important to your query filtering, then you can always just query that collection for the _id values that match and use an $in query on the "day" documents to only match days that contain those matched "quote" documents.

    It's a big decision where it matters most which path you take based on how your application uses the data. Hopefully this should guide you on the general concepts behind doing what you want to achieve.

    P.S Unless you are "sure" that your source data is always a date rounded to an exact "minute" then you probably want to employ the same kind of date rounding math as used to get the discrete "day" as well.

    0 讨论(0)
提交回复
热议问题