mongodb

Partition data around a match query during aggregation

你。 提交于 2021-02-05 11:18:30
问题 What I have been trying to get my head around is to perform some kind of partitioning(split by predicate) in a mongo query. My current query looks like: db.posts.aggregate([ {"$match": { $and:[ {$or:[{"toggled":false},{"toggled":true, "status":"INACTIVE"}]} , {"updatedAt":{$gte:1549786260000}} ] }}, {"$unwind" :"$interests"}, {"$group" : {"_id": {"iid": "$interests", "pid":"$publisher"}, "count": {"$sum" : 1}}}, {"$project":{ _id: 0, "iid": "$_id.iid", "pid": "$_id.pid", "count": 1 }} ]) This

Update MongoDB field using value of another field

不羁岁月 提交于 2021-02-05 11:12:32
问题 In MongoDB, is it possible to update the value of a field using the value from another field? The equivalent SQL would be something like: UPDATE Person SET Name = FirstName + ' ' + LastName And the MongoDB pseudo-code would be: db.person.update( {}, { $set : { name : firstName + ' ' + lastName } ); 回答1: The best way to do this is in version 4.2+ which allows using of aggregation pipeline in the update document and the updateOne, updateMany or update collection method. Note that the latter has

Update MongoDB field using value of another field

﹥>﹥吖頭↗ 提交于 2021-02-05 11:10:32
问题 In MongoDB, is it possible to update the value of a field using the value from another field? The equivalent SQL would be something like: UPDATE Person SET Name = FirstName + ' ' + LastName And the MongoDB pseudo-code would be: db.person.update( {}, { $set : { name : firstName + ' ' + lastName } ); 回答1: The best way to do this is in version 4.2+ which allows using of aggregation pipeline in the update document and the updateOne, updateMany or update collection method. Note that the latter has

How to load data in MongoDB running in host from inside a Docker running on the same machine?

谁说胖子不能爱 提交于 2021-02-05 10:51:27
问题 I am running a Pytorch docker container by the following command in a Ubuntu 18.02 machine: # Run Pytorch container image docker run -it -v /home/ubuntu/Downloads/docker_work/test_py_app/app:/workspace/app -p 8881:8888 -p 5002:5002 --gpus all --rm nvcr.io/nvidia/pytorch:20.08-py3 On the same machine I've a MongoDB running, which has the following details: database_name = 'data_analytics' collection_name = 'TestDB' server = 'localhost' mongodb_port = 27017 I'm running the below code outside

How to find specific nested objects without knowing the parent key in mongodb

半腔热情 提交于 2021-02-05 10:46:10
问题 I'm using javascript and the mongoose module. I have an object like this my_object = { ShopName: String, employees: [String], info: { NameEmployee_1: { age: String, work: String, city: String, }, NameEmployee_2: { age: String, work: String, city: String, } } } and i want find all the emoplyees that have a specific age but without knowing the name of the emplyee, is there a way for do this? I know that you for example i can so something like this db.collection.find({'info.Max': {$exists: true}

How to find specific nested objects without knowing the parent key in mongodb

孤者浪人 提交于 2021-02-05 10:46:07
问题 I'm using javascript and the mongoose module. I have an object like this my_object = { ShopName: String, employees: [String], info: { NameEmployee_1: { age: String, work: String, city: String, }, NameEmployee_2: { age: String, work: String, city: String, } } } and i want find all the emoplyees that have a specific age but without knowing the name of the emplyee, is there a way for do this? I know that you for example i can so something like this db.collection.find({'info.Max': {$exists: true}

I want my pre('save') mongoose function to operate only once

谁说胖子不能爱 提交于 2021-02-05 10:30:49
问题 I do not know if the exact request in title is possible, but if not; i would really appreciate an alternate solution. I have this pre save method of mongoose ownerSchema.pre("save", function(next) { const owner = this; bcrypt.genSalt(10, function(err, salt) { bcrypt.hash(owner.password, salt, function(err, hash) { // Store hash in your password DB. owner.password = hash; next(); }); }); }); When i save new user(owner) a hash is created successfully and all is good> The problem occurs when i

I want my pre('save') mongoose function to operate only once

半城伤御伤魂 提交于 2021-02-05 10:29:28
问题 I do not know if the exact request in title is possible, but if not; i would really appreciate an alternate solution. I have this pre save method of mongoose ownerSchema.pre("save", function(next) { const owner = this; bcrypt.genSalt(10, function(err, salt) { bcrypt.hash(owner.password, salt, function(err, hash) { // Store hash in your password DB. owner.password = hash; next(); }); }); }); When i save new user(owner) a hash is created successfully and all is good> The problem occurs when i

I want my pre('save') mongoose function to operate only once

落花浮王杯 提交于 2021-02-05 10:28:17
问题 I do not know if the exact request in title is possible, but if not; i would really appreciate an alternate solution. I have this pre save method of mongoose ownerSchema.pre("save", function(next) { const owner = this; bcrypt.genSalt(10, function(err, salt) { bcrypt.hash(owner.password, salt, function(err, hash) { // Store hash in your password DB. owner.password = hash; next(); }); }); }); When i save new user(owner) a hash is created successfully and all is good> The problem occurs when i

How to group arrays based on another array in mongodb aggregation?

给你一囗甜甜゛ 提交于 2021-02-05 09:42:38
问题 I have seen kind of a similar question, but it has no answer so it doesn't answer my question. How to group arrays based on another array in mongodb aggregation? Assume you have these schemas: PollSchema const PollSchema = new mongoose.Schema({ numberOfChoices: { type: Number, required: true, min: [1, "Number of choices must be at least 1"], max: [10, "Number of choices cannot be more than 10"], }, arrayOfNamesOfChoices:[{ name: { type: String, required: true, }, }], }); CastedVotedSchema