logo
down
shadow

Update field in mongoDB with it's substring


Update field in mongoDB with it's substring

By : Ahman Hida
Date : November 22 2020, 04:01 AM
it should still fix some issue I would like to shorten all long descriptions in my object. I have searched and read many articles but I can't figure out how to do this simple task in mongoDB. What I am trying to achieve would be simple in SQL: , This should work:
code :
db.AssetDocument.find({
  $where: "this.description.length > 500"
}).forEach(function(e){
  e.description = e.description.substr(0,500);
  db.AssetDocument.save(e);
});


Share : facebook icon twitter icon
How to find a substring in a field in Mongodb

How to find a substring in a field in Mongodb


By : sargie992
Date : March 29 2020, 07:55 AM
hope this fix your issue How can I find all the objects in a database with where a field of a object contains a substring? , Instead of this:
code :
db.database.find({A: {$regex: '/^*(abc def)*$/''}})
db.database.find({A: /abc def/i })
Mongodb query on substring of a field

Mongodb query on substring of a field


By : wawa
Date : March 29 2020, 07:55 AM
wish of those help You can use aggregation $project to create department code column. Then find by $match your condition
code :
    db.collection.aggregate(
            [ 
                { $project:{
                    "cp" : 1,
                    department_code: { $substr: ["$cp", 0, 2]}
                    }
                }
                ,{ $match : { department_code: 'department code to find' } }
            ]
        );
Check if field is a substring of a longer string with MongoDB

Check if field is a substring of a longer string with MongoDB


By : user1715230
Date : March 29 2020, 07:55 AM
help you fix your problem After a quite a bit of trial and error, I've found a way to achieve what I wanted. By using $indexOfBytes in a $gt, I was able to check if trigger existed as a substring in the search string by seeing if the result of $indexOfBytes was greater than -1. Here is my final Mongoose query:
code :
Collection.find({
    $expr: {
        $gt: [
            {
                $indexOfBytes: [
                    search,
                    "$trigger"
                ]
            },
            -1
        ]
    }
});
How to create field from substring of another field in MongoDb?

How to create field from substring of another field in MongoDb?


By : soni
Date : March 29 2020, 07:55 AM
I hope this helps you . if you have mongodb server v4.2 or newer you can do it easily like so:
code :
db.mycollection.updateMany({}, [
    {
        "$set": {
            "field2": {
                "$substr": ["$field1", 0, 2]
            }
        }
    }
])
using MongoDB.Entities;
using MongoDB.Entities.Core;

namespace StackOverflow
{
    public class Item : Entity
    {
        public string field1 { get; set; }
    }

    public class Program
    {
        private static void Main()
        {
            new DB("test", "localhost");

            (new[] {
              new  Item {field1 = "abcdef"},
              new  Item {field1 = "qwerty"},
              new  Item {field1 = "opuytj"}
            }).Save();

            DB.Update<Item>()
              .Match(_ => true)
              .WithPipelineStage("{$set : { field2 : { $substr: [ '$field1', 0, 2 ] } }}")
              .ExecutePipeline();
        }
    }
}
MongoDB update: Generate new field based on existing field, or update in place

MongoDB update: Generate new field based on existing field, or update in place


By : Anthony Chen
Date : March 29 2020, 07:55 AM
seems to work fine Sometimes simply writing the question out brings you to a solution. Kinda hacky, and definitely weird, but seems to work:
Related Posts Related Posts :
  • I am not able to execute aggregate operation on mongoDB using Groovy
  • Very slow start up for Spring Boot tests with Embed Mongo DB
  • How to use Spark MongoDB-Connector with conditional query ?
  • How to write query with LIKE in MongoDB?
  • Get index of array when filter in mongodb
  • Get first embedded document in an array
  • Same shards across different MongoDB collections
  • what is alternative for AggregationOutput to perform lookup operation in mongoDB
  • How to define nested array in Mongoose Schema
  • MongoDB in Go in relation to JSON and BSON
  • MongoDB aggregation - choose a lookup collection depending on the data. Can it be done?
  • How do I disable GridFS MD5 calculation in Spring Boot?
  • $group to get all related data in array
  • Mongoose: $sum in $project return only zero
  • Mongo Shell - Console/Debug Log
  • Relations in Document-oriented database?
  • How do I perform the SQL Join equivalent in MongoDB?
  • shadow
    Privacy Policy - Terms - Contact Us © bighow.org