logo
down
shadow

Javascript - Collect data from mongodb from multiple collections then merge into array


Javascript - Collect data from mongodb from multiple collections then merge into array

By : Vincent Petruzzella
Date : October 22 2020, 08:10 AM
will help you I want to fetch the newest object from multiple collections and save all into an array with collection name as key and the newest object as value. , You could try using async/await as:
code :
let dat = ["test", "test2"];

const promises = dat.map(async (collName) => {
    try {
        const data = await db.collection(collName)
             .find({})
             .sort({ timestamp: -1 })
             .limit(1)
             .toArray();
        return data[0];
    } catch(err) {
        console.error(err);
    }
});

(async () => {
    const merged = await Promise.all(promises);
    console.log(merged);
})();


Share : facebook icon twitter icon
Getting data from multiple collections in mongodb

Getting data from multiple collections in mongodb


By : UserOverflow
Date : March 29 2020, 07:55 AM
hop of those help? Populate is likely what you're looking for:
http://mongoosejs.com/docs/populate.html
code :
var mongoose = require('mongoose')
 , Schema = mongoose.Schema

var PersonSchema = new Schema({
  name    : String
  , age     : Number
  , stories : [{ type: Schema.ObjectId, ref: 'Story' }]
});

var StorySchema = new Schema({
  _creator : { type: Schema.ObjectId, ref: 'Person' }
  , title    : String
  , fans     : [{ type: Schema.ObjectId, ref: 'Person' }]
});

var Story  = mongoose.model('Story', StorySchema);
var Person = mongoose.model('Person', PersonSchema);
Mongodb:Right way to collect data from two collections?

Mongodb:Right way to collect data from two collections?


By : Composite Dynamic Li
Date : March 29 2020, 07:55 AM
it fixes the issue Update to MongoDB 3.2 and you'll be able to use the $lookup aggregation stage, which works similarly to SQL joins.
One-to-many relationship
code :
db.items.aggregate([
{
    $lookup:
    {
        from: "user_item_history",
        localField: "_id",
        foreignField: "item_id",
        as: "item_history"
    }
},
{
    $project:
    {
        item_id: 1,
        status: "$item_history.status",
        imageUrl: 1
    }
}])
{
    "_id" : NumberLong(26220),
    "imageUrl" : "0bd2838e-9349-432a-a200-6e6b659e853eitemcompressed.jpg",
    "status" : [ 1 ]
},
{
    "_id" : NumberLong(26233),
    "imageUrl" : "0bd2838e-9349-432a-a200-6e6b659e853eitemcompressed.jpg",
    "status" : [ 1, 2 ]
}
db.items.aggregate([
{
    $lookup:
    {
        from: "user_item_history",
        localField: "_id",
        foreignField: "item_id",
        as: "item_history"
    }
},
{
    $unwind: "$item_history"
},
{
    $project:
    {
        item_id: 1,
        status: "$item_history.status",
        imageUrl: 1
    }
}])
{
    "_id" : NumberLong(26220),
    "imageUrl" : "0bd2838e-9349-432a-a200-6e6b659e853eitemcompressed.jpg",
    "status" : 1
}
db.items.aggregate([
{
    $lookup:
    {
        from: "user_item_history",
        localField: "_id",
        foreignField: "item_id",
        as: "item_history"
    }
},
{
    $unwind: "$item_history"
},
{
    $match:
    {
        "item_history.user_id": NumberLong(25416),
        "item_history.catagory": "FOOD"
    }
},
{
    $project:
    {
        item_id: 1,
        status: "$item_history.status",
        imageUrl: 1
    }
}])
Merge data of two collections in Mongodb

Merge data of two collections in Mongodb


By : vishnu
Date : March 29 2020, 07:55 AM
I hope this helps you . after going through the documents I was able to merge it, thanks for thoes who commented :)
code :
function new(req, res, next) {
    db.arts.findOne({
        _id: mongoskin.helper.toObjectID(req.params._id)
    }, function(err, art) {
        if (err) return next(err);
        if (!art) {
            return res.status(404).send({
                status: '404 file not found'
            });
        }
        db.users.findOne({
            _id: mongoskin.helper.toObjectID(art.user_id)
        }, function(err, user) {
            if (err) return next(err);
            if (!user) {
                return res.status(404).send({
                    status: '404 User not found'
                });
            }
            return res.send({
                user,art
            });

        })
    });
}
Efficiently collect data from 2 large DataFrames and merge it in to a numpy array

Efficiently collect data from 2 large DataFrames and merge it in to a numpy array


By : Kalon Acharjee
Date : March 29 2020, 07:55 AM
it fixes the issue I have 2 large dataframe's which are too big for merge. Both contain user log data for different months. I need to efficiently collect all data for each id, and create an img/array. , I will do it with groupby get_group
code :
gmacth=df_march.groupby('id')
gfeb=df_feb.groupby('id')
gmacth=df_march.groupby('id')
df=pd.concat([gfeb.get_group(8),gmacth.get_group(8)]).sort_values(by=['date'], ascending=[False])
img = StandardScaler().fit_transform(df[['d1','d2','d3']])
Get counts by merge two collections with conditions in spring data mongodb data

Get counts by merge two collections with conditions in spring data mongodb data


By : Jess W
Date : March 29 2020, 07:55 AM
it helps some times You can try below spring aggregation query in spring mongo 2.x and Mongo 3.4 version.
code :
Instant startofMonth = LocalDate.now()
                .with( TemporalAdjusters.firstDayOfMonth() ).atStartOfDay().toInstant(ZoneOffset.UTC);
Instant endofMonth = LocalDate.now()
                .with( TemporalAdjusters.lastDayOfMonth() ).atTime(LocalTime.MAX).toInstant(ZoneOffset.UTC);

MatchOperation matchOperation = Aggregation.match(Criteria.where("adState").is("ACTIVE").and("agentName").is("Agent_2"));
LookupOperation lookupOperation = LookupOperation.newLookup().
                from("contains").
                localField("adNumber").
                foreignField("adNumber").
                as("result_ad");
UnwindOperation unwindOperation = Aggregation.unwind("result_ad");
MatchOperation matchOperation2 = Aggregation.match(Criteria.where("result_ad.containState").is("ACTIVE").and("result_ad.lastModifiedDate").gte(startofMonth).lte(endofMonth));
CountOperation countOperation = Aggregation.count().as("count");

Aggregation aggregation = Aggregation.newAggregation(matchOperation, lookupOperation, unwindOperation, matchOperation2, countOperation);
Integer results = mongoOperations.aggregate(aggregation, colname, Document.class).getUniqueMappedResult().getInteger("count");
Related Posts Related Posts :
  • Chrome-Extension/JavaScript How to make all links on page have no effect?
  • Websocket issue Internet Explorer
  • sessionStorage value becomes null in other tab in AngularJS
  • Split URL from position x to the end in Javascript
  • How to set value of input field with NgIf in Angular 4
  • invalidStateError for second time screenshare on Chrome
  • How do you pass a value to the variable in a xhr.setRequestHeader?
  • I cant start nodemon on my windows 10
  • Nested if else jumps out from the main if else in javascript
  • Iterate through all collections and remove them
  • ES6 - Value of a variable in template string does not increment
  • how to get content of html table cell using javascript javascript depending on checkbox
  • Node.js Working with Multiple Tier SQL
  • Can't allow Cross-Origin Request in local Nodejs server
  • Datatables orderable=false directly on table
  • Dynamically load js content with iron router in meteor using array in return statement
  • Limit checkbox to 2, but moving the select instead of blocking it
  • Why itsn't displaying marker on map?
  • jQuery target next of the same class in the whole DOM
  • encoding must be a valid encoding error in process encoding
  • How to do a date range to view data from selected date highcharts
  • How do I refactor this composed function with Ramda.js?
  • jQuery to serialize selected option's value, not label
  • Convert object to Chartis.js structure
  • Dynamic import with not bundled file
  • Click handler not firing for elements rendered in Ajax Callback - only in Safari
  • Math.max.apply gives an error CreateListFromArrayLike called on non-object
  • get the parent nodes to a given node in d3 dendogram
  • is-dropdown-submenu-parent makes links unclickable on Opera and Chrome
  • JS: Sum column values and updating result on change
  • Query against nested object keys
  • Fullcalendar getting its parent colspan
  • How to retrieve a dynamic number of elements by id
  • How to access redux variables and functions from deep components
  • Efficiently access Array.prorotype.filter single-item returns
  • PHP var into Javascript
  • How to parse json with javascript/jquery if content matches string
  • How can I make a pyramid based on alphabet?
  • Javascript sorting a csv rows and displaying in table
  • How to get specific data from json from api using ajax. The json looks strange to me
  • react js html5 video not working
  • Javascript Angular 4 Change ngClass from another Component
  • Hide multiple table columns with javascript
  • Convert datetime to ISO Format using moment js
  • Dynamically adjust canvas size depending on content
  • setTimeout function with document on click
  • Need advice on nested Promises.all
  • Google Captcha expiration time
  • Syntax of Javascript TAPE unit test
  • Parse php json to javascript
  • Write to Internet Explorer javascript console with vba in excel
  • 2 Forms with Jquery
  • Why the loop stops right in the middle?
  • filter system for restaurant booking
  • how to add active class in framework nav menu when (add/edit) pages are opened
  • Port a js map in dart
  • How to get value from select using javascript
  • Audio won't be muted with JavaScript - removing audio tags with mutationobserver needed
  • How to include an HTML/Javascript game into reactJS website
  • Convert Nodejs server into node module
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org