1

I have the current document:

{
    "_id" : ObjectId("5c95b81b10ce2a953215c269"),
    "name" : "Œufs & Cie",
    "city" : "Brossard",
    "state" : "QC",
    "postal_code" : "J4Y 0G4",
    "review_count" : 12,
    "categories" : "Restaurants, Breakfast & Brunch, Canadian (New), American (Traditional)",
    "attributes" : [
        {
            "GoodForKids" : "True",
            "Alcohol" : "'none'",
            "Ambience" : "{'touristy': False, 'hipster': False, 'romantic': False, 'intimate': False, 'trendy': False, 'upscale': False, 'classy': False, 'casual': False}",
            "HasTV" : "False",
            "RestaurantsPriceRange2" : "2",
            "RestaurantsAttire" : "'casual'",
            "NoiseLevel" : "'average'",
            "Caters" : "False",
            "BikeParking" : "True",
            "RestaurantsDelivery" : "False",
            "OutdoorSeating" : "True",
            "RestaurantsTakeOut" : "True",
            "RestaurantsGoodForGroups" : "True",
            "RestaurantsReservations" : "False",
            "WiFi" : "'no'"
        }
    ],
    "hours" : [
        {
            "Monday" : "6:0-15:0",
            "Tuesday" : "6:0-15:0",
            "Wednesday" : "6:0-15:0",
            "Thursday" : "6:0-15:0",
            "Friday" : "6:0-15:0",
            "Saturday" : "7:0-16:0",
            "Sunday" : "7:0-16:0"
        }
    ]
}

I want to be able to split the "Ambience" field in "attributes" into a an array. However, I have tried this command:

db.list.aggregate([{$project:{attribute:{$split:["$attributes.Ambience", ", "]}}}])

but I get the error that$split requires a string but it found an array.

I have also tried:

db.list.aggregate([{$project:{attributes.Ambience:{$split:["$attributes.Ambience”, ", "]}}}])

And I got another error saying that I'm missing a ":" after property id @ 1:40.

Does anyone know how I can split the a string in an array into another array? Or how to dynamically find all the possible strings that can be turned into an array so I can turn them into an array?

**UPDATE: I checked the type of attributes.Ambience by running the command:

db.list.aggregate( [ { "$project": { "fieldType": { "$type": "attributes.Ambience" } } } ])

and I got the result :

{ "_id" : ObjectId("5c95b81910ce2a953215362a"), "fieldType" : "string" }

And what I want the resulting attributes.Ambience field to look like is

"attributes" : [
            {
                ...
                "Ambience" : 
                [
                    {
                       "touristy": False, 
                       "hipster": False,
                       "romantic": False, 
                        etc....
                    }
                ],
                ...
            }
  • As per your document Ambience field if of document type, it is not a string. Also please post your expected output look of the new document. – Mani Mar 23 at 18:50
  • I have updated the question @Mani – tom dinh Mar 23 at 19:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.