$split error message: requires an expression that evaluates to a string

Hi
I’m trying to do some simple aggregation query trying to catch all movies with a 1-word title…!

attached my code:

[{$project: {

  Stings:{$split:["$title"," "]},
Filter:{$eq: [{$size: {$split:["$title"," "]}}, 1]},


}}, {$match: { Filter:true}
}, {$group: {
 _id: null, n: { $sum: 1 } 
}}]

Hello, @Tal_Shainfeld! Welcome to the community!

You did not provide any data example, so I will make one:

db.test1.insertMany([
  { _id: 'A', title: "Harry Potter" },
  { _id: 'B', title: "It" },
  { _id: 'C', title: "Star Wars" },
  { _id: 'D', title: "Punisher" },
]);

To match/count documents with one-word title you may not use the aggregation, if you trim the titles before insert documents:

db.test1.find({
  title: {
    $not: {
      $regex: / /,
    },
  },
});

db.test1.count({
  title: {
    $not: {
      $regex: / /,
    },
  },
});

If you do not trim inserted document, then you may do it during the aggregation:

db.test1.aggregate([
  {
    $addFields: {
      title: {
        $trim: {
          input: '$title',
        }
      }
    }
  },
  {
    $match: {
      title: {
        $not: {
          $regex: / /,
        },
      },
    }
  }
]);

You can also use solution without regex, similar to what were trying to do:

db.test1.aggregate([
  {
    $addFields: {
      titleParts: {
        $split: [{
          $trim: {
            input: '$title'
          },
        }, ' '],
      }
    }
  },
  {
    $match: {
      titleParts: {
        $size: 1
      }
    }
  },
  {
    $project: {
      titleParts: false,
    }
  }
]);

Hi Slava
Thanks a lot for swift replay.

  1. Unfortunately, I must use $split & $size since I’m doing some Lab on the MongoDB Academy - aggregation frame and these are the instructions.
  2. I must say that more then I would like to solve the drill I would like to understand the problem on Compass. Meaning why once I add the last stage - {$group: {
    _id: null, n: { $sum: 1 } I got an error message referring to $split - the first stage, Till then All the stages perfectly work!
    I got answers for String and Filter, Then Match is perfect, but once I tried to add the last stage group I got an error messageimage
    Please advise
    Thanks again
    Tal

Probably, some movie in your collection contains non-string title, with number (int), instead of expected string.
You can try to convert all incoming titles to a string type with $toString pipeline operator:

{
  $addFields: {
    titleParts: {
      $split: [{
        $toString: '$title',
      }, ' ']
    }
  }
}

Also, If your question is related to your MongoDB university courses, better to ask this in dedicated discussions for your course.

Hello @Tal_Shainfeld

since this is a MongoDB University Question it would be best to answer this question in the M121 University community forum. There you will get help from teaching assistants and co-students which can also learn by reading and following your question.

Further info: $split

Cheers,
Michael

Hi Slava
OK, Thanks a lot!

Hi Slava
I think there is another problem
I have some code that runs perfectly. But when I add the last stage $count. there is an error message refer to 2 stages before (that was run perfectly!! before I add the last stage $count ), the error message said: it is wrong to subtract a date from a string. Which there is no way I do, since as you can see-before subtraction I change the string to date type with $tostring command.
Seems that compass produces unrelated error messages when there is some problem on the last stage $count.

[{$match: {

    _id:"76001652"

}}, {$unwind: 
 {
  path: "$Agent_Policy_Data",

   }
}, {$project: {
Policy_Id:{$concat:["$Agent_Policy_Data.POLICY_NO"," "]}, 
 dateDifference:{$toInt:{ $divide: [{ $subtract: [ new Date(),{$toDate:("$Agent_Policy_Data.NEXT_RENEWAL_DATE")} ] },1000*60*60*24]}} ,
init:{ $lte:[{ $subtract: [ new Date(),{$toDate:("$Agent_Policy_Data.NEXT_RENEWAL_DATE")} ] },30]}

 }}, {$match: {
  init:false
}}, {$count: "POLICY_VERSION_NO"}]

Please post the actual error message. Are you running this code from mongo shell? I tried your $project followed by the $count stages in mongo shell (see the pipeline stages below). It works fine without any errors.

{ 
  $project: {
    Policy_Id: { $concat:[ "$Agent_Policy_Data.POLICY_NO"," "] },
    dateDifference: { 
      $toInt: { 
	    $divide: [ 
	      { $subtract: [ 
		    new Date(), 
		    { $toDate: ("$Agent_Policy_Data.NEXT_RENEWAL_DATE") } ] 
		  }, 
		  10006060*24 
	    ] 
      } 
    },
    init: { 
      $lte: [ 
	    { $subtract: [ new Date(), { $toDate:("$Agent_Policy_Data.NEXT_RENEWAL_DATE") } ] }, 30 
	  ] 
    }
  } 
 },
{ 
  $count: "POLICY_VERSION_NO" 
}

image
Same code on NoSQLbuster is running perfect, but get an error massage on Mong Compass…
Thanks

What are the versions of the MongoDB and the Compass you are using?

Hi
Compass 1.21.2
Mongo 4.0.9
TX

I am also using the same Compass version and MongoDB v4.2.8. I tried the the same aggregation stages posted in my earlier post, and it works without any errors - in Compass.

Can you provide a sample input document you are working with?

Sorry mongo 4.2 the shell is 4.0.9

{
	"_id" : "76000361",
	"Agent_Summary" : [
		{
			"NO_OF_POLICIES" : "1",
			"COMMISSION_BOOKED_FYP" : "73.14300000000000000000000000000000000000",
			"MONTH" : "July",
			"GWP_NEW_BUSINESS" : "3483.00000000000000000000000000000000000000",
			"YEAR" : "2018",
			"GWP_RENEWAL" : "0E-38",
			"GWP_ACTUAL" : "3483.00000000000000000000000000000000000000",
			"COMMISSION_BOOKED_RYP" : "0E-38"
		},
		{
			"NO_OF_POLICIES" : "1",
			"COMMISSION_BOOKED_FYP" : "0E-38",
			"MONTH" : "May",
			"GWP_NEW_BUSINESS" : "0E-38",
			"YEAR" : "2018",
			"GWP_RENEWAL" : "0E-38",
			"COMMISSION_BOOKED_RYP" : "0E-38"
		},
	
		{
			"NO_OF_POLICIES" : "2",
			"COMMISSION_BOOKED_FYP" : "0E-38",
			"MONTH" : "April",
			"GWP_NEW_BUSINESS" : "0E-38",
			"YEAR" : "2020",
			"GWP_RENEWAL" : "0E-38",
			"COMMISSION_BOOKED_RYP" : "0E-38"
		},
		{
			"NO_OF_POLICIES" : "1",
			"COMMISSION_BOOKED_FYP" : "0E-38",
			"MONTH" : "March",
			"GWP_NEW_BUSINESS" : "0E-38",
			"YEAR" : "2020",
			"GWP_RENEWAL" : "0E-38",
			"COMMISSION_BOOKED_RYP" : "0E-38"
		}
	],
	"Agent_Policy_Data" : [
	  	
		{
			"VERSION_EFF_TO_DATE" : "2019-07-27 00:00:00.000000",
			"POLICY_STATUS" : "678",
			"NEXT_RENEWAL_DATE" : "2020-07-25 00:00:00.000000",
			"POLICY_NO" : "7620482",
			"POLICY_END_DATE" : "2019-07-27 00:00:00.000000",
			"POLICY_VERSION_NO" : "7639348",
			"FIRST_ISSUE_DATE" : "2018-07-28 00:00:00.000000",
			"VERSION_EFF_FROM_DATE" : "2018-07-28 00:00:00.000000",
			"ANNUALIZED_PREMIUM" : "3000.00000000000000000000000000000000000000",
			"RENEWED_IND" : "Renewed",
			"PREMIUM_AMOUNT" : "300.30000000000000000000000000000000000000",
			"BUSINESS_TYPE" : "New Business",
			"RENEWAL_NUMBER" : "2",
			"LAST_RENEWED_DATE" : "2019-06-27 00:00:00.000000",
			"COMMISSION_AMOUNT" : "73.14300000000000000000000000000000000000",
			"POLICY_START_DATE" : "2018-07-28 00:00:00.000000"
		},
		
		{
			"VERSION_EFF_TO_DATE" : "2019-09-04 00:00:00.000000",
			"POLICY_STATUS" : "456",
			"BUSINESS_TYPE" : "New Business",
			"POLICY_NO" : "5615475",
			"POLICY_END_DATE" : "2019-09-04 00:00:00.000000",
			"POLICY_VERSION_NO" : "5630854",
			"FIRST_ISSUE_DATE" : "2018-09-05 00:00:00.000000",
			"POLICY_START_DATE" : "2018-09-05 00:00:00.000000",
			"VERSION_EFF_FROM_DATE" : "2018-09-05 00:00:00.000000"
		},
		{
			"VERSION_EFF_TO_DATE" : "2019-09-04 00:00:00.000000",
			"POLICY_STATUS" : "456",
			"BUSINESS_TYPE" : "Endorsement",
			"POLICY_NO" : "5615475",
			"POLICY_END_DATE" : "2019-09-04 00:00:00.000000",
			"POLICY_VERSION_NO" : "5630855",
			"FIRST_ISSUE_DATE" : "2018-09-05 00:00:00.000000",
			"POLICY_START_DATE" : "2018-09-05 00:00:00.000000",
			"VERSION_EFF_FROM_DATE" : "2018-09-05 00:00:00.000000"
		},
		{
			"VERSION_EFF_TO_DATE" : "2020-09-01 00:00:00.000000",
			"POLICY_STATUS" : "456",
			"NEXT_RENEWAL_DATE" : "2021-08-31 00:00:00.000000",
			"POLICY_NO" : "7605481",
			"POLICY_END_DATE" : "2020-09-01 00:00:00.000000",
			"POLICY_VERSION_NO" : "7610930",
			"FIRST_ISSUE_DATE" : "2019-09-03 00:00:00.000000",
			"VERSION_EFF_FROM_DATE" : "2019-09-03 00:00:00.000000",
			"ANNUALIZED_PREMIUM" : "5000.00000000000000000000000000000000000000",
			"RENEWED_IND" : "Renewed",
			"PREMIUM_AMOUNT" : "559.70000000000000000000000000000000000000",
			"BUSINESS_TYPE" : "Endorsement",
			"RENEWAL_NUMBER" : "2",
			"LAST_RENEWED_DATE" : "2020-08-03 00:00:00.000000",
			"COMMISSION_AMOUNT" : "117.53700000000000000000000000000000000000",
			"POLICY_START_DATE" : "2019-09-03 00:00:00.000000"
		},
		{
			"VERSION_EFF_TO_DATE" : "2019-07-27 00:00:00.000000",
			"POLICY_STATUS" : "456",
			"NEXT_RENEWAL_DATE" : "2020-07-25 00:00:00.000000",
			"POLICY_NO" : "7620482",
			"POLICY_END_DATE" : "2019-07-27 00:00:00.000000",
			"POLICY_VERSION_NO" : "7639348",
			"FIRST_ISSUE_DATE" : "2018-07-28 00:00:00.000000",
			"VERSION_EFF_FROM_DATE" : "2018-07-28 00:00:00.000000",
			"ANNUALIZED_PREMIUM" : "3000.00000000000000000000000000000000000000",
			"RENEWED_IND" : "Renewed",
			"PREMIUM_AMOUNT" : "348.30000000000000000000000000000000000000",
			"BUSINESS_TYPE" : "New Business",
			"RENEWAL_NUMBER" : "2",
			"LAST_RENEWED_DATE" : "2019-06-27 00:00:00.000000",
			"COMMISSION_AMOUNT" : "73.14300000000000000000000000000000000000",
			"POLICY_START_DATE" : "2018-07-28 00:00:00.000000"
		}
		
	]
}

@Tal_Shainfeld,

The aggregation posted here works fine (both in the mongo shell and the Compass). I am including it here. Try to use this one and see what the result is. It is possible, you are using some quotes (or special characters in your aggregation) which are not compatible.

db.collection.aggregate([
{ $match: { _id : "76000361" } },
{ $unwind: { path: "$Agent_Policy_Data" } },
{ 
  $project: {
    Policy_Id: { $concat:[ "$Agent_Policy_Data.POLICY_NO"," "] },
    dateDifference: { 
      $toInt: { 
	    $divide: [ 
	      { $subtract: [ 
		    new Date(), 
		    { $toDate: ("$Agent_Policy_Data.NEXT_RENEWAL_DATE") } ] 
		  }, 
		  10006060*24 
	    ] 
      } 
    },
    init: { 
      $lte: [ 
	    { $subtract: [ new Date(), { $toDate:("$Agent_Policy_Data.NEXT_RENEWAL_DATE") } ] }, 30 
	  ] 
    }
  } 
 },
{ 
  $count: "POLICY_VERSION_NO" 
}
] )

Hi
No, I run the code as I sent to you. Till the stage I’m using $count everything run perfect , But when add that stage I got this error .I don’t get the error run the code on NoSQLBooster .
BTW I tried to use
{ $group: { _id: null, myCount: { $sum: 1 } } }, instead the $count - get the same error !!!
TX
Tal