Checking a string if it's a number or alphanumeric

I have a field sometimes it has numbers only and sometimes it has a string of alphanumeric values like 12b or 3a or 23b etc. I want to test the field to see if it has a number then I will just return that number. But if after testing it, it has an alphanumeric value I would like to split the letters and the numbers. I hope the explanation makes sense. Don’t hesitate to ask for clarification. Many thanks

Hi @Elhadji_M_Ba welcome to the community.

Could you provide an example of what you need? Also, are you looking to do this using the mongo shell, or using some language (node, python, etc.)? An input -> output example would be helpful here.

As an immediate answer, you should be able to do this using regular expressions. See $regex for MongoDB’s operator, or Regular Expression if you’re not familiar with the subject.

Best regards,
Kevin

ok here is an example. I hope it helps. thanks

//Current - numeric portion can be 1 or more digits

[{
  "agendanum": "70"
},{
  "agendanum": "70a",
  "agendasubject": "RIGHTS OF THE CHILD"
},{
  "agendanum": "70b",
  "agendasubject": "CHILDREN--UN. GENERAL ASSEMBLY (27TH SPECIAL SESS. : 2002)"
},{
  "agendanum": "71a",
  "agendasubject": "INDIGENOUS PEOPLES"
},{
  "agendanum": "71b",
  "agendasubject": "INDIGENOUS PEOPLES--CONFERENCE (2014 : NEW YORK)"
}]

//Ideal

[{
  "agendanum": [
	"70"
	]
},{
  "agendanum": [
	"70",
	"a"
	]
  "agendasubject": "RIGHTS OF THE CHILD"
},{
  "agendanum": [
	"70",
	"b"
	]
  "agendasubject": "CHILDREN--UN. GENERAL ASSEMBLY (27TH SPECIAL SESS. : 2002)"
},{
  "agendanum": [
	"71",
	"a"
	]
  "agendasubject": "INDIGENOUS PEOPLES"
},{
  "agendanum": [
	"71",
	"b"
	]
  "agendasubject": "INDIGENOUS PEOPLES--CONFERENCE (2014 : NEW YORK)"
}]